你应该使用
@property (copy) NSString *thumbImage;
并仅保存图像名称而不是
@property (strong) UIImage *thumbImage;
然后编码/编码为字符串。当您需要图像时,只需编写
[UIImage imageNamed:album1.thumbImage];
另一种解决方案是继承UIImage类,添加图片路径属性,重写UIImage的初始化方法以支持保存路径,重写coder/encoder方法
编辑:
这是一些示例代码:
UIImageWithPath.h
#import <UIKit/UIKit.h>
@interface UIImageWithPath : UIImage{
NSString* filepath;
}
@property(nonatomic, readonly) NSString* filepath;
-(id)initWithImageFilePath:(NSString*) path;
@end
UIImageWithPath.m
#import "UIImageWithPath.h"
@implementation UIImageWithPath
@synthesize filepath;
-(id)initWithImageFilePath:(NSString*) path{
self = [super initWithContentsOfFile:path];
if(self){
[filepath release];
filepath = [path copy];
}
return self;
}
-(void)dealloc{
[filepath release];
[super dealloc];
}
@end
示例使用:
- (void)viewDidLoad
{
[super viewDidLoad];
img = [[UIImageWithPath alloc] initWithImageFilePath:[[NSBundle mainBundle] pathForResource:@"pause" ofType:@"png"]];
iv.image = img;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"image file path %@", img.filepath);
}
所以现在你的代码/编码方法应该是这样的:
-(void)encodeWithCoder:(NSCoder *)coder {
NSString *path = _thumbImage.filepath;
[coder encodeObject:(path) forKey:@"thumbImageData"];
}
- (id)initWithCoder:(NSCoder *)coder {
NSString *path = [coder decodeObjectForKey:@"thumbImageData"];
_thumbImage = [[UIImageWithPath alloc] initWithImageFilePath:path];
return self;
}