Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a copy of an UIImageView instance?

How do I create a copy of an UIImageView instance that can be manipulated independently of the first instance?

I've tried UIImageView *tempCopy = [instance copy] but it crashes. Is there another way?

like image 887
John Avatar asked Jan 07 '11 02:01

John


2 Answers

It probably crashes because UIImageView doesn't conform to the NSCopying protocol. So do it yourself: instantiate a new UIImageView and set any property from your original that you find of interest.

like image 107
mvds Avatar answered Oct 12 '22 11:10

mvds


UIImageView doesn't conform to NSCopying, but it does conform to NSCoding. Archive your view, and then de-archive it to get a brand new copy.

For the lazily inclined, this looks like:

NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:imageView];
UIImageView *copy = [NSKeyedUnarchiver unarchiveObjectWithData:data];
like image 37
Mike Abdullah Avatar answered Oct 12 '22 11:10

Mike Abdullah