UIView implements the NSCoding protocol, so you can use encodeWithCoder: to get the serialized view and initWithCoder: to restore the UIView from that view. For details, see Serialization Programming Guide for Cocoa .
Here is a brief example of how to do this:
- (NSData *)dataForView:(UIView *)view { NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:view forKey:@"view"]; [archiver finishEncoding]; [archiver release]; return (id)data; } - (UIView *)viewForData:(NSData *)data { NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; UIView *view = [unarchiver decodeObjectForKey:@"view"]; [unarchiver finishDecoding]; [unarchiver release]; return view; }
Louis Gerbarg
source share