简体   繁体   中英

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!

现在,问题已解决,这是我的财产名称存在的问题,但是感谢您帮助我清理代码!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM