Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7819895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:06:17+00:00 2026-06-02T07:06:17+00:00

I have an object imageView1. I want to create another object to keep the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:06:18+00:00Added an answer on June 2, 2026 at 7:06 am

    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 NSCoding on UIImage and place the following code:

    UIImage+NSCoder.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage (NSCoding)
    - (id)initWithCoder:(NSCoder *)decoder;
    - (void)encodeWithCoder:(NSCoder *)encoder;
    @end
    

    UIImage+NSCoder.m

    #import "UIImage+NSCoder.h"
    
    @implementation UIImage (NSCoding)
    - (id)initWithCoder:(NSCoder *)decoder {
        NSData *pngData = [decoder decodeObjectForKey:@"PNGRepresentation"];
        [self autorelease];
        self = [[UIImage alloc] initWithData:pngData];
        return self;
    }
    - (void)encodeWithCoder:(NSCoder *)encoder {
        [encoder encodeObject:UIImagePNGRepresentation(self) forKey:@"PNGRepresentation"];
    }
    
    @end
    

    Then add a new category with name DeepCopy (for ex.) on UIImageView and the following code:

    UIImageView+DeepCopy.h

    #import <UIKit/UIKit.h>
    
    @interface UIImageView (DeepCopy)
    -(UIImageView *)deepCopy;
    @end
    

    UIImageView+DeepCopy.m

    #import "UIImageView+DeepCopy.h"
    #import "UIImage+NSCoder.h"
    
    @implementation UIImageView (DeepCopy)
    
    -(UIImageView *)deepCopy
    {
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:self forKey:@"imageViewDeepCopy"];
        [archiver finishEncoding];
        [archiver release];
    
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        UIImageView *imgView = [unarchiver decodeObjectForKey:@"imageViewDeepCopy"];
        [unarchiver release];
        [data release];
    
        return imgView;
    }
    
    @end
    

    Usage:
    Import your UIImageView+DeepCopy.h

    UIImageView *imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture.jpg"]];
    UIImageView *imageView2 = [imgView1 deepCopy];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object oriented program and I want to stop execution and maintain
I have this object: const myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "f":{
I have an object model that contains several lists of other objects. I want
I have array with colors as its objects. I want to create an image
I have a singleton object is a card I already defined I want to
Hello i have a uiscrollview and i have one object of uiimageview inside the
I want to have a pointer to an UIImage view so that I don't
I have a dictionary with three objects, and I want to add this dictionary
I have an object MObject that is currently defined like so: + (id)objectWithType:(NSString*)type name:(NSString*)name
I have object A which contains multiple instances of object B, which in turn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.