[英]Can't load images from “Documents” folder in iPhone/iPhone Simulator
我的某個部分應該將圖像保存到手機的“文檔”文件夾中,然后進行訪問。 這樣做可以節省費用,但是加載方法實際上並不起作用。 這是我的.m代碼:
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:
@"photo.png" ];
UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
studentImage = [[UIImageView alloc] initWithImage: image]; //UIImageView property
}
- (UIImage*)loadBarcode
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"barcode.png" ];
NSLog(@"%@", path);
UIImage *image = [UIImage imageWithContentsOfFile:path];
return image;
barcode = [[UIImageView alloc] initWithImage: image]; // UIImageView property
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadBarcode];
[self loadImage];
}
文件肯定在那里可以訪問,文件路徑正確。 但是我試圖將它們分配給UIImageViews空白。 我沒有其他選擇了。 你們對這可能有什么建議嗎?
在loadImage
方法中,問題在這里:
return image;
studentImage = [[UIImageView alloc] initWithImage: image];
您首先使用return
語句,該語句將轉移控件,並且永遠不要將其分配給UIImageView
控件。
應該是這樣的:
studentImage = [[UIImageView alloc] initWithImage: image];
return image;
還有一點,如果studentImage
是通過IB連接的,請DON'T re-allocate it
。 而是使用:
studentImage.image = image;
希望能幫助到你!
現在,問題已解決,這是我的財產名稱存在的問題,但是感謝您幫助我清理代碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.