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

标签 ios objective-c xcode nsdata

我有一个贴纸类,我正在将贴纸对象保存到 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 初始化时它为空或不存在,它将通过调用 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 以供 Parse 下载的 NSData,但现在无论如何它都可以使用。

关于ios - 将 NSMutable 字典保存到文档时遇到问题。 UIImage 不会保存/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872973/

相关文章:

ios - 有人可以帮我解决内存泄漏问题吗?

ios - 调用在类中初始化的字典数组

c# - 使用 Xamarin iOS 获取可用的应用程序内存

ios - 从模态视图 Controller 返回到 RootViewController

ios - 在 Swift 中声明 Objective-C 方法时出现问题

xcode - : -fno-objc-arc 中的未知选项字符 `f'

ios - 如何根据四个可能的值生成随机 SKSpriteNode

ios - 从另一个应用程序在后台启动 iOS 应用程序

objective-c - 对指向接口(interface) 'NSDate' 的指针进行算术运算,这在非脆弱 ABI 中不是常量大小

ios - xCode unity-iphone 需要配置文件