- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法从使用工厂方法创建的 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
在 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 12 中,NSKeyedArchiver 的初始化程序 init(forWritingWith:) 已被弃用。 Xcode 10 建议用新的初始化程序 init(requiringSecu
您好,我需要将 NSKeyedArchiver 类加载到 C++/CLI 对应项。有没有办法获取 NSKeyedArchiver 的内部格式? 另一种选择是将整个保存和打开代码重写为适用于 Mac 和
我有一个有时会更改的对象列表,并且我希望每当应用程序关闭或移动到后台时都在设备上保留持久缓存。列表中的大多数对象都不会改变,所以我想知道保存列表的最佳方法是什么。我考虑了两个主要选择: 使用 NSKe
所以我实现了这两个函数来在我的应用程序中保存数据: - (NSString*)itemArchivePath { NSArray* documentDirectories = NSSearchP
根据我的理解,完整的对象图是使用以下方式存储的: NSKeyedArchiver archiveRootObject 并且按照保存的顺序检索它,这是正确的吗? 我正在获取一个 NSDictionary
@界面 @interface Patient : NSObject @实现 - (id)initWithCoder:(NSCoder *)decoder{ self.patientPhoto
所以我有一个包含一些自定义类的数组: 带游戏的 NSMutableArray (FirstViewcontroller)- 骰子游戏 (Game.m)-- 带选项的骰子 (Dices.m) 在每个自定
我似乎无法让 NSKeyedArchiver 工作。我有一个主 ViewController,它有一个 NSMutableArray 作为实例变量。该数组由自定义类的多个实例填充。自定义类符合NSCo
因此,我的应用程序查询 Amazon Dynamo DB 数据库并检索几千字节的数据。我希望应用程序做的是第一次下载所有内容,然后每次都下载时间戳以查看它是否具有最新版本的数据。所以我只需要每隔一段时
我正在使用 Objective-C 构建一个应用程序,我想保留数据。我在 NSKeyedArchiver 和核心数据之间犹豫不决。我知道网上有很多关于此的资源(包括 Objective-C best
我在使用 NSKeyedArchiver 时遇到问题。我正在尝试归档一个字典,它又包含自定义对象、专辑和歌曲的数组。 Album 和 Song 都是 NSObject 固有的,并且符合 NSCodin
我的应用使用自定义类作为其数据模型: class Drug: NSObject, NSCoding { // Properties, methods etc... } 我刚刚创建了一个 Tod
我目前正在调查一个据称通过 NSKeyedArchiver 存储的文件解档期间发生的崩溃。崩溃日志包含文件的前 8 个字节四部分(下面我只包括前 2 个)。 Fatal Exception: NSIn
我正在用下面的做IOS App数据备份 NSKeyedArchiver.archiveRootObject() NSKeyedUnarchiver.unarchiveObjectWithFile()
我在使用 NSKeyedArchiver.archivedData 时遇到 unrecognized selector sent to instance 错误。 我试图将解析的 JSON 输出存储到自
我正在尝试使用 NSKeyedArchiver 来“归档”一个简单的字典。键是 String 类型,值是 enum。 以下示例说明了我正在尝试做的事情: import Foundation enum
我是否需要担心在后台线程中使用 archiveRootObject 和 unarchiveObjectWithFile? dispatch_async(dispatch_get_global_queu
注意:Xcode 8 Beta 6 我不确定我遗漏了什么,但出于某种原因,我在 NSKeyedArchiver.archivedData 行上收到以下错误。 'NSInvalidArgumentExc
我有一个希望保存和加载的自定义类。该类包含一个 NSDate、一个 NSString 和一个 NSNumber。我已经在 .h 文件中实现了 NSCoding 协议(protocol)。这是我到目前为
我正在编写一个使用 NSKeyedArchiver 来保存其数据的应用程序。我的文件应该是 ishotTrack.plist 还是 ishotTrack.arch? 最佳答案 使用 NSKeyedAr
我是一名优秀的程序员,十分优秀!