• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:38:17 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个贴纸类,我正在将贴纸对象保存到 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-推荐答案


仅供其他人引用,问题在于没有首先将 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap