I have an object imageView1. I want to create another object to keep the deep copy of imageView1.
Like this,
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
UIImageView *imageView2 = [imageView1 copy];
I know it does’t work, because UIImageView doesn’t conform to NSCopying. But how should
i do?
To create a deep copy of UIImageView object you need to archive it using NSKeyedArchiver and then unarchive it with NSKeyedUnarchiver, but there is a problem with this approach because UIImage does not conform to the NSCoding protocol. What you need to do first is to extend the UIImage class to support NSCoding.
Add a new category with name
NSCodingonUIImageand place the following code:UIImage+NSCoder.h
UIImage+NSCoder.m
Then add a new category with name
DeepCopy(for ex.) on UIImageView and the following code:UIImageView+DeepCopy.h
UIImageView+DeepCopy.m
Usage:
Import your UIImageView+DeepCopy.h