gpt4 book ai didi

core-data - 保存 [UIColor colorWithPatternImage :image] UIColor to Core Data using NSKeyedArchiver

转载 作者:行者123 更新时间:2023-12-02 10:20:17 26 4
gpt4 key购买 nike

我无法从使用工厂方法创建的 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.'

最佳答案

哦,是的!我得到了它。在以下人员/帖子的帮助下...

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 类别中调用原始的 UIColorencodeWithCoder 和 initWithCoder 方法。那么你甚至不需要为核心数据编写自己的值转换器,因为 UIColor 实现了 NSCoding 协议(protocol)。代码如下...

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

关于core-data - 保存 [UIColor colorWithPatternImage :image] UIColor to Core Data using NSKeyedArchiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7456835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com