我正在尝试使用NSCoding和NSKeyedArchiver将图像保存到我的Documents文件夹中。该应用程序的工作方式如下:用户拍照,查看预览,保存图像,然后可以在图库部分查看图像。我不会同时保存多个图像。
我遇到了崩溃,如果我的照片类设置不当,或者我的解密代码错误,我不知道我是否正确归档图像。最终,我需要抓取对象并使用它们来填充集合视图。
当我调用代码来加载对象时,我得到一个看起来像这样的崩溃
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [ImageForArchiving countByEnumeratingWithState:objects:count:]:无法识别的选择器发送到实例0x174232da0' ***第一次抛出调用堆栈: (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:以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;
@end
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];
}
}