我有一部分应该将图像保存到手机的“文档”文件夹中,然后进行访问。它可以很好地保存,但加载方法并不真正起作用。这里是我的.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呈现空白。我没有选择。你们有什么建议吗?
当您手动转到文件路径并打开图像时,它是否真的有内容? – TMob 2014-10-09 06:21:03
你有返回类型UIImage的方法,并且你没有使用这些方法的返回对象。此外,这些方法中的最后一行将不会被执行,因为您在最后一行之前返回。 – 2014-10-09 06:24:20
返回图像声明应该在您的代码中将图像设置为imageview后最后。因为在调用函数时不需要返回语句。使该功能为(void) – Max 2014-10-09 06:25:23