在将 NSMutable 字典保存到文档中时遇到问题。UIImage不会保存/显示



我有一个贴纸类,我正在将贴纸对象保存到NSMutableDictionary中。贴纸类别如下:

#import "Sticker.h"
@implementation Sticker

-(instancetype)initWithTitle:(NSString *)title stickerNO:(int)stickerNO image:(UIImage *)image {

    self=[super init];
    if(self){
        self.title=title;
        self.stickerNO=[NSNumber numberWithInt:stickerNO];;
        self.image=image;
    }

    return  self;
}

//CODING METHODS//////////////

-(void)encodeWithCoder:(NSCoder *)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)initWithCoder:(NSCoder *)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是init,如果它是空的或不存在,它将通过调用addDefaultStickers创建并用默认的贴纸填充字典。这适用于我在代码中的一个贴纸。我可以加载和恢复字典,并使用NSLog检查内容。贴纸在那里,但由于某种原因,UIImage为空,我无法显示它。我真的不确定为什么,我已经为它使用了encodeWithCoder,所以它不应该工作吗?奇怪的是,如果我从Parse.com下载一个贴纸(我在上面有一个相同的类),然后将该图像从NSData转换为png并保存它就可以了。有人能告诉我这里可能出了什么问题吗?感谢

编辑这是我从Parse.com下载的代码:

- (void)getNewStickersWithCompletionHandler:(stickerCompletionHandler)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。不知道上面是什么。。

仅供他人参考,问题在于没有首先将UIImage转换为NSData。我将aCoder方法中的UIImage行更改为以下行:

[aCoder encodeObject:UIImagePNGRepresentation(self.image) forKey:@"image"];

在aDecoder:self.image = [UIImage imageWithData:[aDecoder decodeObjectForKey:@"image"]];

我不完全理解为什么它适用于之前被转换为解析下载的UIImage的NSData,但现在一切都正常了。

最新更新