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

标签 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.'

最佳答案

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

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/

相关文章:

ios - 将值从 UITableViewCell 传递到详细信息 View

iphone - 应用更新后保留核心数据

iphone - 如何以位显示 NSData 的内容?

ios - 更改 UIActivityViewController 的背景颜色

ios - 如何使用一对多关系将一个对象附加到 CoreData 中的另一个对象?

ios - 我如何从 NSUserDefaults 存档这个 Swift 字典并通过邮件发送它?编码似乎乱码

ios - 内存泄漏 - NSData 和 NSMutableString

uinavigationcontroller - 更改半透明黑色 UINavigationBar 的颜色

ios - Objective-C - 当 iOS 13 深色模式更改时,以编程方式更改 UIViewController 的渐变背景颜色

ios - -[UITableView _endCellAnimationsWithContext :] with NSFetchedResultsController 中的断言失败