【问题标题】:Proper way of saving and loading pictures保存和加载图片的正确方法
【发布时间】:2011-07-06 12:19:56
【问题描述】:

我正在制作一个小应用程序,用户可以在其中创建游戏配置文件,输入一些数据和可以用相机拍摄的照片。

我在NSUserDefaults 的帮助下保存了大部分个人资料数据,但一位朋友不鼓励我将个人资料图片保存在NSUserDefault 中。

在我的应用中本地保存和检索图像的正确方法是什么?

【问题讨论】:

    标签: ios objective-c cocoa-touch uiimage


    【解决方案1】:

    您应该将其保存在 DocumentsCache 文件夹中。这是怎么做的。

    保存Documents文件夹:

    NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/myImage.png"];
    
    BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path 
              contents:nil attributes:nil];
    
    if (!ok) 
    {
        NSLog(@"Error creating file %@", path);
    } 
    else 
    {
        NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
       [myFileHandle writeData:UIImagePNGRepresentation(yourImage)];
       [myFileHandle closeFile];
    }
    

    加载来自Documents文件夹:

    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
    

    您也可以使用 UIImageJPEGRepresentation 将您的 UIImage 保存为 JPEG 文件。更何况如果你想把它保存在Cache目录中,使用:

    [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/"]
    

    【讨论】:

    • 不要使用[NSHomeDirectory() stringByAppendingString:...]。将[[NSSearchPathForDirectoriesInDomains(..., NSUserDomainMask, YES) objectAtIndex:0]NSDocumentDirectoryNSCachesDirectory 一起使用。
    • 为什么不应该使用[NSHomeDirectory() stringByAppendingString:...]?你能解释一下吗?
    • @José 和 Rafal:NSSearchPathForDirectoriesInDomains 更好,因为如果 Apple 决定更改其目录结构,它不会中断。一般来说,硬编码越少越好。如果有特定的 API 函数可用于您想要做的事情,请使用它。获得这条路径的一个干净的方法是:NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [NSString pathWithComponents:[NSArray arrayWithObjects:dir, @"myImage.png", nil]];(太糟糕了,它太冗长了!)
    • 哎呀,刚刚找到了一种更好的方式来表达我的示例的第二个语句:NSString *path = [dir stringByAppendingPathComponent:@"myImage.png"];
    • @reecon 事实上,你不会发现所有的东西都写在文档中。我所做的声明是基于一些分析结果(使用 Xcode Instruments)。此外,我并不是说调用一次或两次或更多时会占用大量内存,但是,可以说,每次加载 tableview 单元格时都尝试调用它。它最终会显示一些分配峰值。就我而言,它达到了 300 Mb,我已设法将其减少到几 Kb。
    【解决方案2】:

    一种方法是使用应用程序的文档目录。这是特定于应用程序的,不会对其他应用程序可见。
    如何创建这个:
    只需向 App Delegate 添加一个静态函数,然后在需要路径的地方使用该函数。

    - (NSString )applicationDocumentDirectory {
        /
         Returns the path to the application's documents directory.
         */ 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        return basePath;
    }
    
    希望它有帮助..

    【讨论】:

      【解决方案3】:

      我认为this("iphone-user-defaults-and-uiimages") 帖子解决了您的问题。不要将 blob 保存到 NSUserDefaults 等属性列表中。在你的情况下,我会直接写入磁盘。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 2014-01-09
        • 2015-10-07
        • 1970-01-01
        相关资源
        最近更新 更多