【问题标题】:Save a UIImage and reused it on UIImageview保存 UIImage 并在 UIImageview 上重用它
【发布时间】:2013-10-14 05:27:17
【问题描述】:

我目前能够拍摄照片或从相机加载并将图像放置在我的应用程序的 UIImageView 中,因为我希望能够有一个按钮来保存图像,所以当我重新加载应用程序时,图像是仍在 UIImageView 中。

我虽然是一个按钮,一旦你加载它并在

中保存图像
-(void)viewDidLoad

有代码来加载已保存的图像,但我不知道如何处理。

任何帮助都会很棒。

【问题讨论】:

    标签: ios objective-c xcode uiimageview save


    【解决方案1】:

    您可以使用UIImagePNGRepresentationUIImageJPGRepresentation 将图像转换为NSData,然后调用NSDatawriteToFile... 方法之一将数据保存到文件中。

    然后当你重启你的应用程序时,你可以通过调用[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]来获取图像

    -- 编辑--

    保存图片

    UIImage *image = ...;
    NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
    [UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
    

    加载图片

    NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
    

    我使用了NSCachesDirectory,因为我假设您不想备份此图像(通过 iCloud 或 iTunes)。如果你这样做了,那么你会想改用NSDocumentDirectory

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多