- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
假设我有这个 Swift 类: class Foo: NSObject, NSCoding { var stringMember: String var intMember: Int
我需要对包含多个自定义 NSObject 实例的数组进行编码类(class)。但是,这样做时,它会返回带有消息的崩溃: -[Person encodeWithCoder:]: unrecognized
import Foundation class C: NSObject, NSCoding { var n: String = "" override init() {
我正在尝试将 NSCoding 协议(protocol)添加到我的自定义类中: class Convo: NSObject, NSCoding { var name: String v
我有一个 costum 对象,它引用了另一个自定义对象。如何在我的 - (void)encodeWithCoder:(NSCoder *)encoder 方法中对一个自定义对象内的自定义对象进行编码?
我有 .h 和 .m 来实现 NSCoding,但是 mutableArray 对象计数总是 0 ... .h #import @interface Favorite : NSObject {
我有一个自定义对象,我想将其保存到磁盘而不是内存。自定义对象有子对象,子对象有子对象等等。这棵树相当广泛。其中一个子对象有 30 多个属性,其中 6 个是子对象本身。 如果我想使用NSCoding,是
我有两个类:Contact 和 Bill。 Contact 有一个类型为 Bill 的数组。 当我坚持使用 NSKeyedArchiver 时,我的联系人保持得很好,但是,它不会保持 Bill 数组。
我有一个带有 NSString 键和 Class (objc_class) 值的 NSDictionary,我想使用 NSCoding 存档这个字典 协议(protocol)。 我如何存档“类”对象?
当使用 NSCoding 或 NSKeyedArchiver 建立父子关系时;我无法在子项中设置对父项的引用,因为它最终会在 Swift Playground 上崩溃。 我想在我的 Child 类中引
我一辈子都弄不明白为什么我会收到此类不符合 NSCoding 协议(protocol)的错误。也许另一双眼睛会有所帮助。我试图添加注释以明确每个函数在做什么。 import Foundation im
我使我的以下 Person 类符合 NSCoding 协议(protocol)。 class Person: NSObject, NSCoding{ var age:Int var h
我正在尝试能够序列化和解码自定义类。该类本身有一个属性,它是一个枚举数组,如下所示: (我已经阅读了如何使用原始值并将其序列化,但我还没有弄清楚如何使用这样的数组来做到这一点)。 enum WeekD
我有一个带有 String 属性和 NSImage 属性的“Thing”对象; Thing 类有 encodeWithCoder: 和 decodeWithCoder: 方法,我可以使用 NSKeye
我正在使用 NSCoding 来存档/取消存档自定义类作为数据持久化的方法。如果对象是 NSObject 的子类,它工作正常,但我也有对象是自定义对象的子类。我是否需要更改 initWithCoder
我有一系列嵌套对象需要通过 NSCoding 协议(protocol)进行处理,以便我可以将顶级对象保存到 NSUserDefaults 中。 这是对象的结构: '讲师'类 NSMutableArra
-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key { NSMutableArray* archiveArray
我在 Swift 中使用 NSCoding 时遇到问题。我试图实现该协议(protocol),但每当我调用我的 save() 方法时都会发生崩溃。 Xcode 在 encodeWithCoder 中将
我正在尝试使用基本的父子对象继承系统编写一个简单的 RPG,该系统被保存并加载到游戏状态中,但是当发生加载时,父级没有数据出现,我正在努力理解为什么。 为了简单起见,我做了一个简单的父子对象继承,其中
我有一个 NSPopUpButtonCell 的自定义子类,以便我可以覆盖其 drawBezelWithFrame:inView: 方法。 目前,我必须使用 initTextCell:pullsDow
我是一名优秀的程序员,十分优秀!