2012-04-03 154 views
2

(对不起,我的英语)保存UIImage在应用程序路径

我需要在应用程序路径中保存图像并在ImageView上加载图像后。

要获取应用程序的路径:

NSString * imagePath = NSHomeDirectory(); 
imagePath = [imagePath stringByAppendingPathComponent:@"/image.png"]; 

后,保存图片:

NSData *imageData = UIImagePNGRepresentation(self.imageView.image); 
[imageData writeToFile:self.imagePath atomically:YES] 

加载保存应用程序路径

self.imageSaved.image = [UIImage imageWithContentsOfFile:self.imagePath]; 

的问题是,在图像图像没有保存到路径中

问候

编辑:

问题解决了,只是我需要改变路径文件

[imagePath stringByAppendingPathComponent:@"Documents/image.png"]; 

谢谢您的回答。

回答

2
NSString *imagePath = NSHomeDirectory(); 
    imagePath = [imagePath stringByAppendingPathComponent:@"/Documents/image.png"]; 

imagePath = [imagePath stringByAppendingPathComponent:@"/Library/image.png"]; 
1

你不能在iOS应用程序上保存。您只能保存到您的文档目录中。

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 

编辑我想我应该说是你应该只将文件保存到文档目录,如果他们都像你创建在你的榜样的一个文件。

+0

我认为你是错只能够保存到文件。只要它位于主目录的子文件夹中,您也可以保存到库或其他文件夹。 – 2012-04-03 15:08:36

+0

好的,你可以这样做,但根据文件系统编程指南“你不应该使用这个目录[Library]作为用户数据文件。” – borrrden 2012-04-03 15:18:10

+0

根据ios数据存储准则/ Documents目录将自动备份iCloud。可以再次下载或重新下载的数据应存储在/Library/Caches目录中。 https://developer.apple.com/icloud/documentation/data-storage/ – 2012-04-04 00:30:23