gpt4 book ai didi

iphone - NSCoding、NSData、UIImages 和性能

转载 作者:行者123 更新时间:2023-11-28 20:37:42 26 4
gpt4 key购买 nike

我正在使用 NSCoding 来管理具有几个不同字段的自定义对象,其中一个是图像。我只针对 iOS 5.0+ 并已切换到 ARC。我的一切正常,但我专注于性能——我还没有看到这样的问题,所以这里是:

我将 UIImage 转换为 NSData 并将其添加到主 NSCoding 文件(一个 plist,如果重要的话)以存储在磁盘上。如果有多张图片,图片名称将按顺序排列(例如 image1、image2、image3)。然后我在 UITableView(作为调整大小的缩略图)和详细 View 中使用该图片。不利的一面是 plist 的大小会膨胀,这意味着当我使用它时初始加载时间很慢,因为它是一次加载所有 NSData。

消除此问题并一次只强制加载一张图片的最佳方法是什么?

我想到的:

我将 NSData 写入磁盘,将数组添加到 plist,并仅将对每个图像的文件名的引用添加到数组。我想我会在指定位置引用图像文件名,在磁盘上找到它并使用它?

欢迎提出任何想法。与其他任何事情相比,我更专注于概念性实现,有趣的是,这不是一个经常讨论的话题。

谢谢,

编辑:

按照下面的要求,这里有一个拍摄图像并将其转换为 NSData 的示例:

  UIImage *originalImage;

NSData *imageData = UIImagePNGRepresentation(originalImage);
//I save it all to the app's document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//I am using the images in a tableView and thus found it easiest to append the row number to each image filename.
//'y' below is just an integer that corresponds to the number of items in the master array

NSString *fileName = [NSString stringWithFormat:@"image%d.png",y];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:fileName];
[imageData writeToFile:documentsDirectory atomically:YES];

// NSLog(@"The filename is %@", fileName);

//newObject is an instance of my NSCoding object
[newObject setImageName: fileName];

最佳答案

您的建议很合理。想象一个关系数据库。我永远不会将一个字段中的图像保存为 blob。文件系统是大量保存二进制日期的好地方。它还为您提供了复制文件等的简便方法。

因此,在您的 plist 中保存一个引用将使解析变得非常快,并且延迟加载成为一项简单的任务,仅在您需要时才处理图像。

关于iphone - NSCoding、NSData、UIImages 和性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9801242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com