【发布时间】:2014-12-03 23:31:57
【问题描述】:
我有一部分应该将图像保存到我手机的“文档”文件夹中,然后访问它。它保存得很好,但是加载方法并没有真正起作用。这是我的 .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。我的选择已经不多了。你们对它可能是什么有什么建议吗?
【问题讨论】:
-
手动去文件路径打开图片,真的有内容吗?
-
您有返回类型为 UIImage 的方法,但您没有使用这些方法的返回对象。这些方法中的最后一行也不会执行,因为您在最后一行之前返回。
-
return image 语句应该在你的代码中将 image 设置为 imageview 之后。而且也不需要 return 语句,因为您在调用函数时没有使用。将该函数设为 (void)
标签: ios objective-c file