2011-09-17 2 views
2

를 사용하여 코어 데이터에 UIColor는저장 [UIColor colorWithPatternImage : 이미지] 난 공장 메서드로 만든 (패턴 포함) UIColor에서있는 NSData 개체를 만들 수 없습니다 해요 NSKeyedArchiver

[UIColor colorWithPatternImage:image]

잘 동작 표준 UIColor 객체 Core Data에 패턴이있는 UIColor를 저장하는 또 다른 방법이 있는지 궁금합니다. 나는 (패턴 포함) UIColor를 보관하려면 다음 코드를 사용하고

...

- (id)transformedValue:(id)value { 
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value]; 
return data; 

}

이러한 내가 받고있어 오류가 있습니다 ...

-[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it. 

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.' 

답변

5

오 예! 알았다. 다음과 같은 사람/글에서 많은 도움 ...

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; 
}
+0

굉장한 ... 당신은 나의 날을 만들었습니다. ... 고마워요. – Ankur

1

UIColor는 NSCoding 프로토콜을 구현하기 때문에 자체 변환기를 작성할 필요가 없지만 코어 데이터에 NSKeyedUnarchiveFromDataTransformerName 변환 (기본값)을 사용하도록 지정할 수 있습니다.

제 생각에이 변환은 UIColor 객체의 패턴을 처리합니다.

+0

여전히 작동하지 않는, 당신이 하나를 지정하지 않을 때 코어 데이터 기본값 변압기를 사용합니다 흥미로운하지만. 표준 UIColor 객체에서는 작동하지만 패턴이있는 UIColor 객체에 대해서는 여전히 동일한 오류가 발생합니다. –

+0

오류가 중복되었습니다. 버그 같아. – TechZen

+0

시도해 주셔서 감사합니다 –

관련 문제