我有一个贴纸类,我正在将贴纸对象保存到 NSMutableDictionary。贴纸类如下:
#import "Sticker.h"
@implementation Sticker
-(instancetype)initWithTitleNSString *)title stickerNOint)stickerNO imageUIImage *)image {
self=[super init];
if(self){
self.title=title;
self.stickerNO=[NSNumber numberWithInt:stickerNO];;
self.image=image;
}
return self;
}
//CODING METHODS//////////////
-(void)encodeWithCoderNSCoder *)aCoder{
//choose what we save, these are objects
[aCoder encodeObject:self.title forKey"title"];
[aCoder encodeObject:self.stickerNO forKey"stickerNO"];
[aCoder encodeObject:self.image forKey"image"];
}
-(instancetype)initWithCoderNSCoder *)aDecoder
{
self=[super init];
if(self){
self.title=[aDecoder decodeObjectForKey"title"];
self.stickerNO=[aDecoder decodeObjectForKey"stickerNO"];
self.image=[aDecoder decodeObjectForKey"image"];
}
return self;
}
@end
字典由 StickerManager 类管理:
@implementation StickerManager
-(instancetype)init {
self = [super init];
//load the dictionary
NSString *path = [self itemArchivePath];
self.stickerDictionary=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
//if there is no dictionary create it and add the default
if(!self.stickerDictionary) {
NSLog(@"Creating dictionary and adding default");
self.stickerDictionary=[[NSMutableDictionary alloc] init];
[self addDefaultStickers];
[self saveStickerDictionary];
}
//if empty fill it
else if ([self.stickerDictionary count]==0){
NSLog(@"Dictionary exists but it empty");
[self addDefaultStickers];
[self saveStickerDictionary];
}
return self;
}
//add the stickers included in the app bundle
-(void)addDefaultStickers {
Sticker *sticker = [[Sticker alloc] initWithTitle“Batman” stickerNO:1 image:[UIImage imageNamed“batman.png"]];
[self.stickerDictionary setObject:sticker forKey:sticker.title];
}
-(BOOL)saveStickerDictionary{
NSLog(@"Saving stickers");
//get the path from above
NSString *path = [self itemArchivePath];
return [NSKeyedArchiver archiveRootObject:self.stickerDictionary toFile:path];
}
-(NSString *)itemArchivePath
{
//get the directory
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//set it to a string
NSString *documentDirectory = [documentDirectories firstObject];
//here we call the file items.archive
return [documentDirectory stringByAppendingPathComponent"stickers.archive"];
}
@end
如果当 StickerManager 为空或不存在时初始化,它将通过调用 addDefaultStickers 创建并使用默认贴纸填充字典。这适用于我在代码中的一个贴纸。我可以加载和恢复字典并使用 NSLog 检查内容。贴纸在那里,但由于某种原因 UIImage 为空,我无法显示它。我真的不知道为什么,我已经使用了 encodeWithCoder 所以它不应该工作吗?奇怪的是,如果我从 Parse.com 下载一个贴纸(我上面有一个相同的类),然后将该图像从 NSData 转换为 png 并保存它就可以了。有人可以给我一些指示,看看这里可能出了什么问题吗?谢谢
编辑这是我从 Parse.com 下载的代码:
- (void)getNewStickersWithCompletionHandlerstickerCompletionHandler)handler
{
__weak StickerManager *weakSelf = self;
PFQuery *stickersQuery = [PFQuery queryWithClassName"sticker"];
[stickersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error) {
for( PFObject *object in objects) {
NSString *title = object[@"title"];
int stickerNO = [[object objectForKey:@"stickerNO"] intValue];
//DOWNLOAD IMAGE CODE
PFFile *image = object[@"image"];
[image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if(!error){
UIImage *image = [UIImage imageWithData:data];
Sticker *sticker = [[Sticker alloc] initWithTitle:title stickerNO:stickerNO image:image];
[weakSelf.stickerDictionary setObject:sticker forKey:sticker.title];
[self saveStickerDictionary];
handler(YES,nil);
}
}];//end get image block
}//end for
}
}];//end download stickers block
}
如果我以这种方式将贴纸保存到字典中,则没有问题,我可以显示图像。这几乎与 addDefaultStickers 相同,我保存的是 UIImage 而不是保存 NSData。不知道这里有什么..
Best Answer-推荐答案 strong>
仅供其他人引用,问题在于没有首先将 UIImage 转换为 NSData。我将 aCoder 方法中的 UIImage 行更改为以下:
[aCoder encodeObject:UIImagePNGRepresentation(self.image) forKey:@"image"];
并在aDecoder中:self.image = [UIImage imageWithData:[aDecoder decodeObjectForKey:@"image"]];
不完全理解为什么它适用于之前转换为用于 Parse 下载的 UIImage 的 NSData,但它现在仍然可以正常工作。
关于ios - 将 NSMutable 字典保存到文档时遇到问题。 UIImage 不会保存/显示,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28872973/
|