ああはい!わかった。以下の人々/ポストから多くの援助と...
Gave me the idea to use associatedObjects
Explanation of associatedObjects
and method swizzling
UIColorのカテゴリを作成します。関連付けられたオブジェクトを使用して、UIColorインスタンスのパターンイメージ(ダイナミックプロパティのようなもの)への参照を設定します。<objc/runtime.h>
をインポートすることを忘れないでください。 UIColor color = [UIColor colorWithPatternImage:selectedImage]
を作成するときには、関連するオブジェクトも色[color setAssociatedObject:selectedImage]
に設定します。
次に、カスタムencodeWithCoderメソッドとinitWithCoderメソッドをカテゴリに実装して、UIImageをシリアル化します。
最後に、UIColorカテゴリ内から元のUIColorのencodeWithCoderメソッドとinitWithCoderメソッドを呼び出すことができるように、main.mファイルにいくつかのメソッドをスウィズします。 UIColorがNSCodingプロトコルを実装しているため、独自のValue Data Transformer for Core Dataを作成する必要はありません。以下のコード...
UIColor + patternArchive
#import "UIColor+patternArchive.h"
#import <objc/runtime.h>
@implementation UIColor (UIColor_patternArchive)
static char STRING_KEY; // global 0 initialization is fine here, no
// need to change it since the value of the
// variable is not used, just the address
- (UIImage*)associatedObject
{
return objc_getAssociatedObject(self,&STRING_KEY);
}
- (void)setAssociatedObject:(UIImage*)newObject
{
objc_setAssociatedObject(self,&STRING_KEY,newObject,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)encodeWithCoderAssociatedObject:(NSCoder *)aCoder
{
if (CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor))==kCGColorSpaceModelPattern)
{
UIImage *i = [self associatedObject];
NSData *imageData = UIImagePNGRepresentation(i);
[aCoder encodeObject:imageData forKey:@"associatedObjectKey"];
self = [UIColor clearColor];
} else {
// Call default implementation, Swizzled
[self encodeWithCoderAssociatedObject:aCoder];
}
}
- (id)initWithCoderAssociatedObject:(NSCoder *)aDecoder
{
if([aDecoder containsValueForKey:@"associatedObjectKey"])
{
NSData *imageData = [aDecoder decodeObjectForKey:@"associatedObjectKey"];
UIImage *i = [UIImage imageWithData:imageData];
self = [[UIColor colorWithPatternImage:i] retain];
[self setAssociatedObject:i];
return self;
}
else
{
// Call default implementation, Swizzled
return [self initWithCoderAssociatedObject:aDecoder];
}
}
main.m
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import "UIColor+patternArchive.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Swizzle UIColor encodeWithCoder:
Method encodeWithCoderAssociatedObject = class_getInstanceMethod([UIColor class], @selector(encodeWithCoderAssociatedObject:));
Method encodeWithCoder = class_getInstanceMethod([UIColor class], @selector(encodeWithCoder:));
method_exchangeImplementations(encodeWithCoder, encodeWithCoderAssociatedObject);
// Swizzle UIColor initWithCoder:
Method initWithCoderAssociatedObject = class_getInstanceMethod([UIColor class], @selector(initWithCoderAssociatedObject:));
Method initWithCoder = class_getInstanceMethod([UIColor class], @selector(initWithCoder:));
method_exchangeImplementations(initWithCoder, initWithCoderAssociatedObject);
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
素晴らしい...あなたは私の一日を作りました...ありがとう – Ankur