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 7669321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:34:04+00:00 2026-05-31T15:34:04+00:00

Prior to iOS 5.1 if you wanted to use NSCoding protocols with UIImage you

  • 0

Prior to iOS 5.1 if you wanted to use NSCoding protocols with UIImage you had to do something like this.

@interface UIImage (NSCoding)

-(id)initWithCoder:(NSCoder *)deocder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

Then implement it yourself. However with iOS 5.1 this generates a warning “Category is implementing a method which will also be implemented by its primary class” in the .M file for this protocol. Now if I remove this extension class iOS5.1 will be happy, however this should crash and burn on iOS4.0. So what is the best course of action?

  • 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-05-31T15:34:05+00:00Added an answer on May 31, 2026 at 3:34 pm

    I have the same issue, and since it is too late to use subclass rather than cateogry, I followed zoul’s suggestion to use class_addMethod, and below is my implementation:

    #import "UIImage-NSCoding.h"
    #include <objc/runtime.h>
    #define kEncodingKey        @"UIImage"
    
    static void __attribute__((constructor)) initialize() {
        @autoreleasepool {
    
            if (![[UIImage class] conformsToProtocol:@protocol(NSCoding)]) {
                Class class = [UIImage class];
    
                if (!class_addMethod(
                                     class,
                                     @selector(initWithCoder:), 
                                     class_getMethodImplementation(class, @selector(initWithCoderForArchiver:)),
                                     protocol_getMethodDescription(@protocol(NSCoding), @selector(initWithCoder:), YES, YES).types
                                    )) {
                                        NSLog(@"Critical Error - [UIImage initWithCoder:] not defined.");
                                    }
    
                if (!class_addMethod(
                                     class,
                                     @selector(encodeWithCoder:),
                                     class_getMethodImplementation(class, @selector(encodeWithCoderForArchiver:)),
                                     protocol_getMethodDescription(@protocol(NSCoding), @selector(encodeWithCoder:), YES, YES).types
                                    )) {
                                        NSLog(@"Critical Error - [UIImage encodeWithCoder:] not defined.");
                                    }
    
            } 
        }
    }
    
    @implementation UIImage(NSCoding)
    
    - (id) initWithCoderForArchiver:(NSCoder *)decoder {
    
        if ((self = [super init]))
        {
            NSData *data = [decoder decodeObjectForKey:kEncodingKey];
            self = [self initWithData:data];
        }
    
        return self;
    
    }
    
    - (void) encodeWithCoderForArchiver:(NSCoder *)encoder {
    
        NSData *data = UIImagePNGRepresentation(self);
        [encoder encodeObject:data forKey:kEncodingKey];
    
    }
    
    @end
    

    So far I have not noticed any further issue. Hope it helps!

    [ATTENTION]

    If you use this UIImage category in order to archive UIImageViewer objects, beware that NSCoding implementation of UIImageViewer in iOS 5 seems to be broken. The image property of the UIImageViewer is lost after save and load, when it is specified in XIB (I have not tried to see if there is the same issue when UIImageViewer object is being created in code). This has been fixed in iOS 6.

    [UPDATES]

    I changed my code to add methods in +load instead initialize(), it is still being executed only once, but earlier. My current implementation:

    #import "UIImage+NSCoding.h"
    #import <objc/runtime.h>
    #define kEncodingKey        @"UIImage"
    
    @implementation UIImage (NSCoding)
    
    + (void) load
    {
    
        @autoreleasepool {
            if (![UIImage conformsToProtocol:@protocol(NSCoding)]) {
                Class class = [UIImage class];
                if (!class_addMethod(
                                     class,
                                     @selector(initWithCoder:), 
                                     class_getMethodImplementation(class, @selector(initWithCoderForArchiver:)),
                                     protocol_getMethodDescription(@protocol(NSCoding), @selector(initWithCoder:), YES, YES).types
                                     )) {
                    NSLog(@"Critical Error - [UIImage initWithCoder:] not defined.");
                }
    
                if (!class_addMethod(
                                     class,
                                     @selector(encodeWithCoder:),
                                     class_getMethodImplementation(class, @selector(encodeWithCoderForArchiver:)),
                                     protocol_getMethodDescription(@protocol(NSCoding), @selector(encodeWithCoder:), YES, YES).types
                                     )) {
                    NSLog(@"Critical Error - [UIImage encodeWithCoder:] not defined.");
                }
    
            } 
        }
    }
    
    - (id) initWithCoderForArchiver:(NSCoder *)decoder {
        if (self = [super init]) {
            NSData *data = [decoder decodeObjectForKey:kEncodingKey];
            self = [self initWithData:data];
        }
    
        return self;
    
    }
    
    - (void) encodeWithCoderForArchiver:(NSCoder *)encoder {
    
        NSData *data = UIImagePNGRepresentation(self);
        [encoder encodeObject:data forKey:kEncodingKey];
    
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Prior to iOS 4.0, CoreLocation was reporting altitude correctly, now it always reports as
Based on the privacy requirements from the ios store, can we include analytics like
Prior to submitting a form, I would like to check if a file has
Does the OS purge the Caches directory prior to iOS 5?
This is a request for guidance regarding Bluetooth accessory development for iOS devices. Publicly
Prior to this question, I asked this question on this site. (Mr Jon Skeet
I want to release UIViewController object in iOS 5. Prior iOS versions (>5.0) we
LOTS of Default.png Q's, but I didn't read one like this: I see Default.png,
Prior to Vista (2003, XP) Windows had a key in the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\dmio\Boot Info\Primary
Prior to ARC, if I wanted a property to be readonly to using it

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.