我尝试使用NSKeyedArchiver archivedDataWithRootObject:dataArray保存一个包含模型的数组,该数组由一个图像和一个字符串组成;但此方法在调用此方法2-3次后返回nil。只有当我尝试归档的模型包含图像时,才会发生这种情况。我检查过数据数组不是空的。下面是我使用的代码:
@implementation ImagesModel
- (id)initWithCoder:(NSCoder *)coder{
if(self = [super init]){
self.image = [[UIImage alloc]initWithData:[coder decodeObjectForKey:@"image"]];
self.urlString = [coder decodeObjectForKey:@"urlString"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:UIImagePNGRepresentation(self.image) forKey:@"image"];
[coder encodeObject:self.urlString forKey:@"urlString"];
}
+ (NSDictionary *)getObjectMapper {
if (!mapperDictionary) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:[PropertyMapping mappingForSimpleTypeWithMappedKeyName:@"image"] forKey:@"image"];
[dictionary setObject:[PropertyMapping mappingForSimpleTypeWithMappedKeyName:@"urlString"] forKey:@"urlString"];
mapperDictionary = [dictionary copy];
}
return mapperDictionary;
}
- (NSData)ArchiveData {
if (!dataArray){
return NO; //nothing to save
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataArray]; //Return Nil
}发布于 2016-07-22 04:10:27
这取决于你如何加载你的镜像。这是一个可能对你有帮助的答案:
How to encode a UIImage in an NSCoder
此外,如果您想要存储.JPEG图像,则可能需要使用UIImageJPEGRepresentation。
https://stackoverflow.com/questions/38511741
复制相似问题