[quote=引用 6 楼 u012668981 的回复:]
[quote=引用 1 楼 ran0809 的回复:] 对象才可以归档 你可以找找NSObject+Autocoding 这个类可以给所有类型归档
[quote=引用 7 楼 yang8456211 的回复:] 你是问CGPoint与UIImage的归档吧,刚才写了个demo都可以啊。 1)自定义的类里面继承NSCoding 与NSCopying,你的point就定义成NSValue。 @property (copy,nonatomic) UIImage *image; @property (copy,nonatomic) NSValue *point; 2)归档的时候就用encodeObject [encoder encodeObject:image forKey:@”ImageKey”]; [encoder encodeObject:point forKey:@”PointKey”]; 3)存的时候 CGPoint point = CGPointMake(1.1, 2.2); NSValue *value = [NSValue valueWithCGPoint:point]; //存储数据到类 Archive *archivingData = [[Archive alloc] init]; archivingData.image = [UIImage imageNamed:@"pic.jpg"]; archivingData.point = value; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:archivingData forKey: @"ArchivingData"]; [archiver finishEncoding]; //写入文件 [data writeToFile:self.archivingFileRootPath atomically:YES];// archivingFileRootPath就是存的路径 4)取得时候 NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self. archivingFileRootPath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //获得类 Archive *archivingData = [unarchiver decodeObjectForKey: @"ArchivingData"]; [unarchiver finishDecoding]; self.imageView.image = archivingData.image; NSValue *value = archivingData.point; CGPoint point = [value CGPointValue]; NSLog(@"%f||%f",point.x,point.y); 当然如果你是做游戏的话,建议你把那些CGPoint都放到NSArray里面吧(转化为NSValue)
你是问CGPoint与UIImage的归档吧,刚才写了个demo都可以啊。 1)自定义的类里面继承NSCoding 与NSCopying,你的point就定义成NSValue。 @property (copy,nonatomic) UIImage *image; @property (copy,nonatomic) NSValue *point; 2)归档的时候就用encodeObject [encoder encodeObject:image forKey:@”ImageKey”]; [encoder encodeObject:point forKey:@”PointKey”]; 3)存的时候 CGPoint point = CGPointMake(1.1, 2.2); NSValue *value = [NSValue valueWithCGPoint:point]; //存储数据到类 Archive *archivingData = [[Archive alloc] init]; archivingData.image = [UIImage imageNamed:@"pic.jpg"]; archivingData.point = value; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:archivingData forKey: @"ArchivingData"]; [archiver finishEncoding]; //写入文件 [data writeToFile:self.archivingFileRootPath atomically:YES];// archivingFileRootPath就是存的路径 4)取得时候 NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self. archivingFileRootPath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //获得类 Archive *archivingData = [unarchiver decodeObjectForKey: @"ArchivingData"]; [unarchiver finishDecoding]; self.imageView.image = archivingData.image; NSValue *value = archivingData.point; CGPoint point = [value CGPointValue]; NSLog(@"%f||%f",point.x,point.y); 当然如果你是做游戏的话,建议你把那些CGPoint都放到NSArray里面吧(转化为NSValue)
对象才可以归档 你可以找找NSObject+Autocoding 这个类可以给所有类型归档
29,028
社区成员
12,466
社区内容
加载中
试试用AI创作助手写篇文章吧