【发布时间】:2012-04-14 00:56:37
【问题描述】:
我正在使用这段代码尝试将图像保存到文档目录:
//FOR TESTING ON SIMULATOR
UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString* imageName = @"Receipt1Image1.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:NO];
self.receiptImage1 = fullPathToFile;
self.receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPathToFile];
NSLog(@"fullPathToFile %@", fullPathToFile);
但是,这段代码并未将 imageViews 图像设置为我尝试写入文档目录的收据图像。
谁能解释我做错了什么以及如何解决它?
谢谢,
杰克
【问题讨论】:
-
你检查
writeToFile:atomically:的返回值了吗?如果有错误,您可以使用writeToFile:options:error:变体检查它是什么。 -
另外,为什么您实际上需要将图像从应用程序包复制到文档目录?
-
您在设备中检查过您的代码吗?因为模拟器不会逐行执行代码..[imageData writeToFile:fullPathToFile atomically:NO];方法需要一些时间将图像写入路径,所以尝试在一段时间后将图像加载到图像中
-
您的代码运行良好。请查看 IBOutlet 以获取receiptImageView1。
-
@MrTJ 成功将图像保存到文档目录后,我将更改代码以从相机/照片中获取图像,这仅用于在模拟器上进行测试。谢谢大家。
标签: iphone objective-c ios uiimageview nsdocumentdirectory