我正在尝试使用 NSCoding 和 NSKeyedArchiver 将图像保存到我的 Documents 文件夹中。该应用程序的工作原理如下:用户拍照,查看预览,保存图像,然后可以在图库部分查看图像。我不会同时保存多个图像。
我遇到了崩溃,不知道我是否错误地存档了图像,我的照片类设置是否不正确,或者我的取消存档代码是否错误。最终我需要抓取对象并使用它们来填充集合视图。
当我调用代码来加载对象时,我遇到了一个看起来像这样的崩溃
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ImageForArchiving countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x174232da0' *** First throw call stack: (0x18533c22c 0x196fb00e4 0x1853432f4 0x1853400a8 0x18524296c 0x10001aefc 0x189ded1ec 0x189dd62c8 0x189decb88 0x189dec814 0x189de5d50 0x189db8f74 0x18a05a124 0x189db7488 0x1852f3f8c 0x1852f3230 0x1852f12e0 0x18521cf74 0x18ec7f6fc 0x189e1ed94 0x10001840c 0x19765aa08) libc++abi.dylib: terminating with uncaught exception of type NSException
我的子类代码如下所示。ImageForArchiving 符合 NSCoder 协议。
ImageForArchiving.h
@interface ImageForArchiving : NSObject <NSCoding>
@property (strong) NSDate *date;
@property (strong) NSData *imageData;
+ (ImageForArchiving *)createImageWithImage:(NSData*)data andDate:(NSDate*)date;
-(void)encodeWithCoder:(NSCoder *)aCoder;
-(id)initWithCoder:(NSCoder *)aDecoder;
@结尾
ImageForArchiving.m
@implementation ImageForArchiving
+ (ImageForArchiving *)createImageWithImage:(NSData*)data andDate:(NSDate*)date {
ImageForArchiving *archiveImage = [[ImageForArchiving alloc] init];
[archiveImage setDate:date];
[archiveImage setImageData:data];
return archiveImage;
}
- (id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
[self setDate: [coder decodeObjectForKey:@"date"]];
[self setImageData: [coder decodeObjectForKey:@"image"]];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:self.date forKey:@"date"];
[coder encodeObject:self.imageData forKey:@"image"];
}
@end
存档代码在预览 VC 中,用户可以在拍摄后查看照片。它显示为从带有块的 segue 中弹出的模式。
PreviewViewController.m (保存图像 IBAction)
NSString *filePath = [self createPathForDataFile];
NSData *pngData = UIImagePNGRepresentation(self.previewImage.image);
NSDate *date = [NSDate date];
ImageForArchiving *imageRecord = [ImageForArchiving createImageWithImage:pngData andDate:date];
[self saveItemsAtFilePath:filePath andImageToArchive:imageRecord];
创建文件路径
- (NSString *)createPathForDataFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
documentsPath = [documentsPath stringByExpandingTildeInPath];
NSError *error = nil;
if ([fileManager fileExistsAtPath: documentsPath] == NO)
{
[fileManager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:&error];
}
return [documentsPath stringByAppendingPathComponent:@"images"];
}
保存图像
- (void)saveItemsAtFilePath:(NSString*)filePath andImageToArchive:(ImageForArchiving*)imageRecord {
[NSKeyedArchiver archiveRootObject:imageRecord toFile:filePath];
}
加载图像 - 这是发生崩溃的地方
- (void)loadItems {
NSString *filePath = [self createPathForDataFile];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
self.galleryItemsArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
} else {
self.galleryItemsArray = [NSMutableArray array];
}
}