我有同样的问题,因为它是来不及使用的子类,而不是cateogry,我也跟着zoul的建议,使用class_addMethod,以下是我的实现:
#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
到目前为止,我还没有发现任何进一步的问题
希望它有帮助!
[注意]
如果为了存档UIImageViewer对象使用UIImage的类别,提防在iOS 5中NSCoding实施UIImageViewer似乎被打破。 UIImageViewer的image属性在保存和加载后会丢失,当它在XIB中指定时(我没有试图查看在代码中创建UIImageViewer对象时是否存在相同的问题)。这已被固定在IOS 6.
[UPDATES]
我改变代码添加在+负载方法,而不是初始化(),仍在只执行一次,但是早。我目前的执行情况:
#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
这个问题已经被问及当时这是我是谁的答案是 - 我不记得是怎样的问题,因为它是早在夏天,但我记得有一个重复这个问题在这里。 – 2012-10-06 08:44:19
啊,这是它:http://stackoverflow.com/questions/11950173/conditional-categories-in-mountain-lion/11950348#11950348 – 2012-10-06 08:50:10