0

我正在尝试用图像归档 ns mustable 数组,我的代码如下所示:

//in my .h file I have
#define kName @"name"
#define kEmail  @"email"
#define kPhone  @"phone"
#define kDetail @"detail"
#define kImages @"images"
#define kRequestDate  @"requestDate"
#define kRequestSent @"status"

@interface BAPRequest : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *email;
@property (nonatomic) NSString *phone;
@property (nonatomic) NSString *detail;
@property (nonatomic) NSMutableArray *images;
@property (nonatomic) NSDate *requestDate;
@property (nonatomic) BOOL requestSent;
@end

//in my .m file I have
#import "BAPRequest.h"

@implementation BAPRequest

@synthesize name, email, phone, detail;
@synthesize images;
@synthesize requestDate;
@synthesize requestSent;

- (void)encodeWithCoder:(NSCoder *)encoder{
  [encoder encodeObject:name forKey:kName];
  [encoder encodeObject:email forKey:kEmail];
  [encoder encodeObject:phone forKey:kPhone];
  [encoder encodeObject:detail forKey:kDetail];
  [encoder encodeObject:images forKey:kImages];
  [encoder encodeObject:requestDate forKey:kRequestDate];
  [encoder encodeBool:requestSent forKey:kRequestSent];
}

- (id)initWithCoder:(NSCoder *)decoder{
  name = [decoder decodeObjectForKey:kName];
  email = [decoder decodeObjectForKey:kEmail];
  phone = [decoder decodeObjectForKey:kPhone];
  detail = [decoder decodeObjectForKey:kDetail];
  images = [decoder decodeObjectForKey:kImages];
  requestDate = [decoder decodeObjectForKey:kRequestDate];
  requestSent = [decoder decodeBoolForKey:kRequestSent];
  return self;
}
@end

当然,我也有 NSKeyedArchiver 代码,就像其他人一样。

其他所有内容都正确存档,当我保存然后加载它们时,似乎一切正常。

但存档图像似乎不是。

我确实有一个 MutableArray 在存档之前存档每个 BAPObjects,想知道是因为 ios 不支持在 MutableArray 中存档 MutableArray 还是仅仅因为我无法轻松存档图像?

干杯菲尔

4

2 回答 2

1

存档图像不要使用

而是使用 by 或将 转换UIImage为表示。NSDataUIImageJPEGRepresentationUIImagePNGRepresentation

然后存档NSMutableArray

编辑:您必须将要保存的项目替换NSMutableArrayNSData

NSData *imageData = UIImagePNGRepresentation([your_array objectAtIndex:0]);

将 UIImage 替换为imageData

于 2012-11-07T18:47:33.280 回答
1

UIImageApple在 iOS 5.1 中添加了归档支持。因此,解决问题的一种方法是将部署目标提升到 iOS 5.1。

如果您不能这样做,请查看UIImage 和 NSCoding iOS 5.1的第一个答案。请注意,给定的实现与 Apple 的归档支持兼容。UIImage

于 2012-11-07T19:44:20.130 回答