0

I am trying to archieve ns mustable array with images, my code looks like:

//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

of course I also have the NSKeyedArchiver codes as well just like everyone else.

Everything else does archive properly, when I save then load them, it seems all working.

But archiving images seems not.

I do have a MutableArray archiving each BAPObjects as well before archive, wonder is it because ios doesn't support archiving MutableArray inside MutableArray or is it just because I can't archive images easily?

Cheers Phil

phil88530
  • 1,499
  • 3
  • 19
  • 27

2 Answers2

1

to archive an image dont use

Instead convert the UIImage to NSData representations using by using UIImageJPEGRepresentation or UIImagePNGRepresentation.

Then archive the NSMutableArray

Edit: You will have to replace the items that you want to save in the NSMutableArray with NSData

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

Replace the the UIImage with the imageData

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • do I have to write my own UIIMage class with the encode or decode, or can I override its encode/decode(if it has any)? It would be brilliant if you can get me little example. – phil88530 Nov 07 '12 at 19:18
  • I've added a UIImage+NSCoding for help encoding the images(from what rob has suggested), but it still doesn't work. Assuming if I added the encoding catagory, it will get compiled, do I have to include the UIImage+NSCoding.h somewhere? – phil88530 Nov 07 '12 at 20:17
  • sorry I am stupid, it works. – phil88530 Nov 07 '12 at 20:24
1

Apple added archiving support to UIImage in iOS 5.1. So one way you can fix your problem is by raising your deployment target to iOS 5.1.

If you can't do that, take a look at the first answer to UIImage and NSCoding iOS 5.1. Be warned that the implementation given there is not forward-compatible with Apple's UIImage archiving support.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • are you suggesting if I simply raise to 5.1, I don't have to do any change? I am currently on 5.0. – phil88530 Nov 07 '12 at 19:48
  • You have to run on an iOS 5.1 device (or simulator). Raising the deployment target to iOS 5.1 will prevent you from running it on an iOS 5.0 device (or simulator). – rob mayoff Nov 07 '12 at 19:51
  • The thing is, I am trying it on my 4s with ios6.1 on already. so that doesn't change the result at all. – phil88530 Nov 07 '12 at 19:55
  • I got the result from your linked answer for iOS 5.0, but simply change it to 5.1 doesn't help – phil88530 Nov 07 '12 at 20:25