2014-10-09 120 views
0

我有一部分应该将图像保存到手机的“文档”文件夹中,然后进行访问。它可以很好地保存,但加载方法并不真正起作用。这里是我的.m代码:无法从iPhone/iPhone模拟器的“文档”文件夹中加载图像

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

} 

这些文件肯定有访问权限,文件路径是正确的。但是我试图将它们分配给它的UIImageView呈现空白。我没有选择。你们有什么建议吗?

+0

当您手动转到文件路径并打开图像时,它是否真的有内容? – TMob 2014-10-09 06:21:03

+0

你有返回类型UIImage的方法,并且你没有使用这些方法的返回对象。此外,这些方法中的最后一行将不会被执行,因为您在最后一行之前返回。 – 2014-10-09 06:24:20

+0

返回图像声明应该在您的代码中将图像设置为imageview后最后。因为在调用函数时不需要返回语句。使该功能为(void) – Max 2014-10-09 06:25:23

回答

0

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; 

希望它有帮助!

+0

这帮助我找到答案!我非常糟糕地搞乱了我的属性,这影响了我的代码。非常感谢你的回答! – 2014-10-09 06:44:51

0

现在问题已修复,是我的属性名称的问题,但感谢您帮助我清理我的代码!

相关问题