gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:15 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com