Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // UIView+cloneSerialization.m
- //
- // Created by Jonathan Annett on 9/10/13.
- // Copyright (c) 2013 Sophtwhere. All rights reserved.
- //
- #import "UIView+cloneSerialization.h"
- #import "NSData+zlibCompression.h"
- @implementation UIView (cloneSerialization)
- -(NSData*) serializedData {
- NSMutableData *data = [[NSMutableData alloc] init];
- NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
- [archiver encodeObject:self forKey:@"view"];
- [archiver finishEncoding];
- NSData* result = [data compress];
- [archiver release];
- [data release];
- return result;
- }
- + (UIView *) UIViewFromSerializedData:(NSData *)data {
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[data decompress]];
- UIView *view = [unarchiver decodeObjectForKey:@"view"];
- [unarchiver finishDecoding];
- [unarchiver release];
- return view;
- }
- + (id) viewFromSerializedData:(NSData *)data {
- UIView *result = nil;
- @autoreleasepool {
- UIView *template = [self UIViewFromSerializedData:data];
- if (template) {
- Class selfClass = [self class];
- id result_ = [[selfClass alloc] initWithFrame:template.frame];
- result = result_;
- result.hidden = template.hidden;
- result.backgroundColor = template.backgroundColor;
- result.alpha = template.alpha;
- result.opaque = template.opaque;
- result.clearsContextBeforeDrawing = template.clearsContextBeforeDrawing;
- result.contentMode = template.contentMode;
- if ([result respondsToSelector:@selector(setTintColor:)]) {
- result.tintColor = template.tintColor;
- }
- if ([result respondsToSelector:@selector(setTintAdjustmentMode:)]) {
- result.tintAdjustmentMode = template.tintAdjustmentMode;
- }
- result.clipsToBounds = template.clipsToBounds;
- while (template.subviews.count>0) {
- UIView *subview = [template.subviews firstObject];
- [subview retain];
- [subview removeFromSuperview];
- [result addSubview:subview];
- [subview release];
- }
- }
- }
- return [result autorelease];
- }
- -(UIView*) cloneAsUIView {
- NSData *data = [self serializedData];
- return [[self class] UIViewFromSerializedData:data];
- }
- -(id) clone {
- NSData *data = [self serializedData];
- return [[self class] viewFromSerializedData:data];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment