繁体   English   中英

无法从iPhone / iPhone模拟器的“文档”文件夹中加载图像

[英]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.

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