当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Can't load images from "Documents" folder in iPhone/iPhone Simulator

问题描述:

I have a part which is supposed to save an image into my phone's "Documents" folder and then access it. It saves fine, but the load methods don't really work. Here's my .m code:

- (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];

}

The files are definitely there to access, the file paths are correct. But the UIImageViews I'm trying to assign them to come up blank. I'm running out of options. Do you guys have any suggestions as to what it might be?

网友答案:

In loadImage method, the problem is here:

return image;
studentImage = [[UIImageView alloc] initWithImage: image];

You first use return statement which transfers the control and never assign it to the UIImageView control.

It should be something like:

studentImage = [[UIImageView alloc] initWithImage: image];
return image;

One more point, if studentImage is connected through IB, DON'T re-allocate it. Instead use:

studentImage.image = image;

Hope it helps!

网友答案:

Problem is fixed now, was an issue with my property names, but thank you for helping me clean up my code!

分享给朋友:
您可能感兴趣的文章:
随机阅读: