sophtwhere

UIView+cloneSerialization.m

Oct 28th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  UIView+cloneSerialization.m
  3. //
  4. //  Created by Jonathan Annett on 9/10/13.
  5. //  Copyright (c) 2013 Sophtwhere. All rights reserved.
  6. //
  7.  
  8. #import "UIView+cloneSerialization.h"
  9. #import "NSData+zlibCompression.h"
  10.  
  11. @implementation UIView (cloneSerialization)
  12.  
  13. -(NSData*) serializedData {
  14.     NSMutableData    *data = [[NSMutableData alloc] init];
  15.     NSKeyedArchiver  *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  16.    
  17.     [archiver encodeObject:self forKey:@"view"];
  18.     [archiver finishEncoding];
  19.    
  20.     NSData* result = [data compress];
  21.    
  22.     [archiver release];
  23.     [data release];
  24.     return result;
  25. }
  26.  
  27. + (UIView *) UIViewFromSerializedData:(NSData *)data {
  28.     NSKeyedUnarchiver  *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[data decompress]];
  29.    
  30.     UIView *view = [unarchiver decodeObjectForKey:@"view"];
  31.    
  32.     [unarchiver finishDecoding];
  33.     [unarchiver release];
  34.    
  35.     return view;
  36. }
  37.  
  38.  
  39. + (id) viewFromSerializedData:(NSData *)data {
  40.     UIView *result = nil;
  41.     @autoreleasepool {
  42.         UIView *template = [self UIViewFromSerializedData:data];
  43.         if (template) {
  44.            
  45.            
  46.             Class selfClass = [self class];
  47.             id result_ = [[selfClass alloc] initWithFrame:template.frame];
  48.             result = result_;
  49.             result.hidden = template.hidden;
  50.             result.backgroundColor = template.backgroundColor;
  51.             result.alpha = template.alpha;
  52.             result.opaque = template.opaque;
  53.             result.clearsContextBeforeDrawing = template.clearsContextBeforeDrawing;
  54.             result.contentMode = template.contentMode;
  55.             if ([result respondsToSelector:@selector(setTintColor:)]) {
  56.                 result.tintColor = template.tintColor;
  57.             }
  58.             if ([result respondsToSelector:@selector(setTintAdjustmentMode:)]) {
  59.                 result.tintAdjustmentMode = template.tintAdjustmentMode;
  60.             }
  61.            
  62.             result.clipsToBounds = template.clipsToBounds;
  63.            
  64.             while (template.subviews.count>0) {
  65.                 UIView *subview = [template.subviews firstObject];
  66.                 [subview retain];
  67.                 [subview removeFromSuperview];
  68.                 [result addSubview:subview];
  69.                 [subview release];
  70.             }
  71.         }
  72.        
  73.     }
  74.    
  75.     return [result autorelease];
  76. }
  77.  
  78. -(UIView*) cloneAsUIView {
  79.     NSData *data = [self serializedData];
  80.     return [[self class] UIViewFromSerializedData:data];
  81. }
  82.  
  83. -(id) clone {
  84.     NSData *data = [self serializedData];
  85.     return [[self class] viewFromSerializedData:data];
  86. }
  87.  
  88.  
  89. @end
Advertisement
Add Comment
Please, Sign In to add comment