我使用NSCoding来管理一个自定义对象与几个不同的领域,其中一个图像。我只针对iOS 5.0+并已切换到ARC。我有一切工作,但我专注于性能 - 我没有看到这样的问题,所以这里它:NSCoding,NSData,UIImages和性能
我将UIImage转换成NSData并将其添加到主NSCoding文件(一个plist,如果它很重要)存储在磁盘上。如果有多个图像,则图像名称将变为顺序的(例如,image1,image2,image3)。然后,我在UITableView(作为调整大小的缩略图)和详细视图中都使用该图像。不利的一面是,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];
是啊,你的存储图像作为单独的文件和文件名中引用它们的想法是什么我会做。如果记录数量足够大,甚至加载所有非图像字段开始花费很长时间,我可能会切换到非图像数据的核心数据。 – yuji 2012-03-21 09:03:38
你有一个将你的UIImage序列化成NSData的快速代码示例吗?这对我正在进行的一个项目非常有用,对其他项目无疑是有用的。 – 2012-07-11 20:02:55