오 예! 알았다. 다음과 같은 사람/글에서 많은 도움 ...
and method swizzling
Explanation of associatedObjects
Gave me the idea to use associatedObjects
이 UIColor에 카테고리를 만들기로. Associated Object를 사용하여 UIColor 인스턴스의 패턴 이미지에 대한 참조 (동적 속성과 같은 종류)를 설정하려면
<objc/runtime.h>
을 반드시 가져와야합니다. UIColor
color = [UIColor colorWithPatternImage:selectedImage]
을 만들 때 색상이
[color setAssociatedObject:selectedImage]
인 관련 객체도 설정합니다.
그런 다음 사용자 지정 encodeWithCoder 및 initWithCoder 메서드를 범주에 구현하여 UIImage를 serialize하십시오.
마침내 main.m 파일에서 몇 가지 방법을 사용하여 UIColor 카테고리에서 원래의 UIColor encodeWithCoder 및 initWithCoder 메소드를 호출 할 수 있습니다. UIColor가 NSCoding 프로토콜을 구현하기 때문에 Core Data 용으로 자체 Value Transformer를 작성할 필요조차 없습니다. 아래 코드 ...
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