使用NSKeyedArchiver将[UIColor colorWithPatternImage:image] UIColor保存到核心数据

时间:2011-09-17 18:11:05

标签: core-data nsdata uicolor nskeyedarchiver

我无法使用工厂方法创建的UIColor(带图案)创建NSData对象

[UIColor colorWithPatternImage:image]

适用于标准UIColor对象。想知道是否有另一种方法可以将具有模式的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.'

3 个答案:

答案 0 :(得分:6)

哦,是的!我知道了。在以下人员/帖子的帮助下...

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。

最后在main.m文件中调用一些方法,以便从UIColor类别中调用原始的UIColor encodeWithCoder和initWithCoder方法。然后你甚至不需要为Core Data编写自己的Value Transformer,因为UIColor实现了NSCoding协议。代码如下......

的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;
}

答案 1 :(得分:1)

由于UIColor实现了NSCoding协议,因此您不必编写自己的转换器,而只需告诉Core Data使用NSKeyedUnarchiveFromDataTransformerName转换(这是默认值)。

据我所知,该变换处理UIColor对象中的模式。

答案 2 :(得分:0)

我确定这是一种将uicolor转换为十六进制字符串的方法,您可以简单地将十六进制字符串存储在核心数据模型中具有“NSString”。

How can I convert RGB hex string into UIColor in objective-c?