【问题标题】:Save UIImage in application path将 UIImage 保存在应用程序路径中
【发布时间】:2012-04-04 08:42:31
【问题描述】:

(对不起我的英语)

我需要将图像保存在应用程序路径中,然后在 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"];

感谢您的回答。

【问题讨论】:

    标签: ios ipad uiimage


    【解决方案1】:
       NSString *imagePath = NSHomeDirectory();
       imagePath = [imagePath stringByAppendingPathComponent:@"/Documents/image.png"];
    

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

    【讨论】:

      【解决方案2】:

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

      NSArray *dirPaths;
      NSString *docsDir;
      
      dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
          NSUserDomainMask, YES);
      
      docsDir = [dirPaths objectAtIndex:0];
      

      编辑我想我应该说的是,如果文件与您在示例中创建的文件类似,您应该只将文件保存到文档目录中。

      【讨论】:

      • 我认为你只能够保存到文档是错误的。您也可以保存到库或其他文件夹,只要它位于主目录的子文件夹中即可。
      • 好的,你可以,但是根据文件系统编程指南“你不应该使用这个目录 [Library] 来存放用户数据文件。”
      • 根据 ios 数据存储指南 /Documents 目录将由 iCloud 自动备份。可以再次下载或重新生成的数据应存储在 /Library/Caches 目录中。 developer.apple.com/icloud/documentation/data-storage
      猜你喜欢
      • 2016-06-05
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2015-01-07
      • 2018-07-05
      • 1970-01-01
      • 2022-06-16
      相关资源
      最近更新 更多