保存[UIColor colorWithPatternImage:image]使用NSKeyedArchiver将UIColor转换为Core数据 [英] Saving [UIColor colorWithPatternImage:image] UIColor to Core Data using NSKeyedArchiver

查看:551
本文介绍了保存[UIColor colorWithPatternImage:image]使用NSKeyedArchiver将UIColor转换为Core数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用工厂方法创建的UIColor(使用模式)创建NSData对象



[UIColor colorWithPatternImage: image]



适用于标准UIColor对象。想知道是否有另一种方法来保存一个带有模式的UIColor到Core Data中。



我使用下面的代码来存档UIColor

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

}



我收到...

   -  [NSKeyedArchiver dealloc]:警告:NSKeyedArchiver在没有调用-finishEncoding的情况下释放。 

由于未捕获异常而终止应用程序'NSInternalInconsistencyException',原因:'只支持RGBA或白色空间,这种方法是一个黑客'
pre>

解决方案

哦!我知道了。有很多来自以下人员/帖子的帮助...



给我使用关联对象的想法



展示相关对象



和方法调换



在UIColor上创建一个类别。使用关联对象来设置对UIColor实例中的模式映像的引用(类似于动态属性),不要忘记导入< objc / runtime.h> 。当创建你的UIColor color = [UIColor colorWithPatternImage:selectedImage] 时,还要在颜色 [color setAssociatedObject:selectedImage] c $ c>。



然后在类别中实现自定义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; //全局0初始化在这里,没有
//需要更改它,因为
//变量的值不使用,只是地址

- (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 {

//调用默认实现,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
{
//调用默认实现,Swizzled
return [self initWithCoderAssociatedObject:aDecoder];
}
}

main.m

  #import< UIKit / UIKit.h> 
#import< objc / runtime.h>
#importUIColor + patternArchive.h



  
int main(int argc,char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Swizzle UIColor encodeWithCoder:
方法encodeWithCoderAssociatedObject = class_getInstanceMethod([UIColor class],@selector(encodeWithCoderAssociatedObject :));
方法encodeWithCoder = class_getInstanceMethod([UIColor class],@selector(encodeWithCoder :));
method_exchangeImplementations(encodeWithCoder,encodeWithCoderAssociatedObject);

// Swizzle UIColor initWithCoder:
方法initWithCoderAssociatedObject = class_getInstanceMethod([UIColor class],@selector(initWithCoderAssociatedObject :));
方法initWithCoder = class_getInstanceMethod([UIColor class],@selector(initWithCoder :));
method_exchangeImplementations(initWithCoder,initWithCoderAssociatedObject);

int retVal = UIApplicationMain(argc,argv,nil,nil);
[pool release];
return retVal;
}


I'm unable to create an NSData object from a UIColor (with a pattern) created with the factory method

[UIColor colorWithPatternImage:image]

works fine for standard UIColor objects. Wondering if there is another way to save a UIColor with a pattern into Core Data.

I am using the following code to archive the UIColor (with a pattern)...

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

}

and these are the errors I'm receiving...

-[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.'

解决方案

Oh Yes! I got it. With a lot of help from the following people/posts...

Gave me the idea to use associatedObjects

Explanation of associatedObjects

and method swizzling

Create a category on UIColor. Use an Associated Object to set a reference to the pattern image in the UIColor instance (kind of like a dynamic property), don't forget to import <objc/runtime.h>. When you create your UIColor color = [UIColor colorWithPatternImage:selectedImage], also set the associated object on the color [color setAssociatedObject:selectedImage].

Then implement custom encodeWithCoder and initWithCoder methods in the category to serialize the UIImage.

And finally do some method swizzling in the main.m file so you can invoke the original UIColor encodeWithCoder and initWithCoder methods from within your UIColor Category. Then you don't even need to write your own Value Transformer for Core Data because UIColor implements the NSCoding protocol. Code below...

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

这篇关于保存[UIColor colorWithPatternImage:image]使用NSKeyedArchiver将UIColor转换为Core数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆