views:

399

answers:

1

I was wondering if it is possible to send a UIImage over bluetooth after encoding it into a NSMutableData using NSKeyedArchiver. This is what I had in mind:

NSMutableData *data = [[NSMutableData alloc] init];

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:[UIImage imageNamed:"test.png" forKey:kImageKey];

[archiver finishEncoding];
+3  A: 

You cannot archive UIImage instances like that. You will have to create an external representation first, like PNG or JPG. You can do that with for example the UIImagePNGRepresentation() function. It will return an NSData instance containing the compressed image in PNG format. That NSData instance can be used with NSArchiving.

St3fan
I guess I would have known this if I'd simply looked at the class reference for UIImage. Thanks!
Ron Dear
Would it be possible to subclass NSObject to create a class that contains an image, name, number etc. and archive that?
Ron Dear
Yes. But using a dictionary is also perfectly fine I think. And it is somewhat safer if you have two different versions of the app. Or at least easier to deal with.
St3fan
Thank you. Needed this.
OscarMk