2011-12-26 31 views
17

Tôi không thể hiển thị hình ảnh (.png) của mình.Tải hình ảnh từ thư mục tài liệu

Tôi kiểm tra xem nó có tồn tại ở đường dẫn tôi sử dụng hay không. Nhưng không có hình ảnh nào xuất hiện.

Dưới đây là những gì tôi đã cố gắng:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentPaths objectAtIndex:0]; 

NSString *outputPath = [documentsDir stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]]; 

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; 



NSString *imgPath = [[outputPath stringByReplacingOccurrencesOfString:@"mov" withString:@"png"]retain]; 


if ([fileManager fileExistsAtPath:imgPath]) 
{ 
    NSLog(@"FOUND IMG"); 
    NSLog(imgPath); 
} 


NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]]; 
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData]; 
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)]; 
[imgView setImage:thumbNail]; 

[cell addSubview:imgView]; 
[imgView release]; 

Sản lượng debug xác nhận tập tin tồn tại

FOUND IMG 
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B- 
9509-EF68B1A0E720/Documents/0.009000.png 

Tôi là một chút bối rối. Có ai thấy tôi đã phạm sai lầm không?

Nhiều Cảm ơn, -Code

Tôi đã thử một vài phương pháp khác, nhưng không bao giờ xuất hiện.

+0

Bạn có thể nhận được câu trả lời của bạn ở đây: http://stackoverflow.com/questions/6663511/iphone-how-to-display-document-directory-images-in- image-view – utsabiem

+0

Để sử dụng 'NSFileManager', sử dụng đơn giản phương thức thuyết phục:' NSFileManager * fileManager = [NSFileManager defaultManager] 'Nói chung, hãy kiểm tra tài liệu cho các phương thức thuyết phục dựa trên lớp. Alss sử dụng ARC nếu có thể, nó thực sự aces phát triển mã đơn giản hơn nhiều. – zaph

Trả lời

16

Bạn nên sử dụng [NSURL fileURLWithPath:imgPath] thay vì [NSURL URLWithString:imgPath]. Đường dẫn hình ảnh không phải là url hợp lệ, nó cần tiền tố file://.

4

Bạn cần phải cung cấp url hồ sơ hợp lệ sẽ được

NSURL *imgURL = [NSURL fileURLWithPath:imgPath]; 

sau đó khởi tạo hình ảnh của bạn với var imgURL thích hợp và cuối cùng là để thêm hình ảnh vào tế bào của bạn, bạn cần phải làm như sau

[cell.contentView addSubview:imgView]; 

Tôi hy vọng nó có thể giải quyết vấn đề của bạn.

21

Cố gắng sử dụng

NSData *imgData = [NSData dataWithContentsOfFile:imgPath]; 
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData]; 

hoặc

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]]; 
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData]; 
+0

Tôi làm cách nào để hiển thị ảnh GIF từ Thư mục Tài liệu? – sohil

0

url của bạn là hoàn hảo.

Bạn cần phải khởi tạo hình ảnh với sau dòng

UIImage *thumbNail=[UIImage imageWithContentsOfFile:imagePath]; 

bây giờ làm

[imgView setImage:thumbNail]; 

[cell addSubview:imgView]; 
3
imageView.image = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename"]]; 
0

Swift 4

Bộ luật này:

extension FileManager { 
    class func getImage(named: String) -> UIImage? { 
     let imagePath = getImagePath(named: named) 
     return UIImage(contentsOfFile: imagePath) 
    } 

    class func getImagePath(named: String) -> String { 
     let fileManager = FileManager.default 
     let documentDir = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) 
     return documentDir.appendingPathComponent(named).path 
    } 
} 

Cách sử dụng:

let image = FileManager.getImage(named: "image.png") 
Các vấn đề liên quan