gpt4 book ai didi

ios - 存储和检索图像数组

转载 作者:行者123 更新时间:2023-11-29 02:02:51 25 4
gpt4 key购买 nike

我在使用 NSUserDefaults 存储和检索图像数组时遇到问题。

var MyStringArray: [String] = []

var MyImageArray: [UIImage] = []

//to store an array of strings

NSUserDefaults.standardUserDefaults().setObject(MyStringArray, forKey: "\(FriendFullname.text!) strings")


//to retrieve an array of strings

let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!) strings")
MyStringArray = tabledata as! [String]


//to store an array of images

NSUserDefaults.standardUserDefaults().setObject(MyImageArray, forKey: "\(FriendFullname.text!) images")


//to retrieve an array of images


let tabledata2 = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!) images")
MyArray = tabledata as! [UIImage]

但是,存储字符串工作正常每次我运行用于存储和检索图像的代码时,应用程序都会崩溃。

我相信这可能是因为这不是保存图像的正确方法。

我使用不同的方式来存储单个图像:

// storing single images

let imageData = UIImageJPEGRepresentation(myImgVw.image!, 1)
let relativePath = "image_\(NSDate.timeIntervalSinceReferenceDate()).jpg"
let path = self.documentsPathForFileName(relativePath)
imageData.writeToFile(path, atomically: true)
NSUserDefaults.standardUserDefaults().setObject(relativePath, forKey: "path")
NSUserDefaults.standardUserDefaults().synchronize()


//retrieving single images

let possibleOldImagePath = NSUserDefaults.standardUserDefaults().objectForKey("path") as! String?
if let oldImagePath = possibleOldImagePath {
let oldFullPath = self.documentsPathForFileName(oldImagePath)
let oldImageData = NSData(contentsOfFile: oldFullPath)
// here is the saved image:
let oldImage = UIImage(data: oldImageData!)
}


// file path

func documentsPathForFileName(name: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
let path = paths[0] as! String;
let fullPath = path.stringByAppendingPathComponent(name)

return fullPath
}

所以我的问题是如何将图像数组存储到 [UIImage] 类型的变量中。

这是我在尝试存储数组时遇到的错误:

2015-05-09 16:16:02.864 Application[3310:767507] Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') 2015-05-09 16:16:02.866 Application[3310:767507] Attempt to set a non-property-list object ( ", {0, 0}" ) as an NSUserDefaults/CFPreferences value for key Frank Rosenberg images 2015-05-09 16:16:02.870 Application[3310:767507] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ( ", {0, 0}" ) for key Frank Rosenberg images' * First throw call stack: (0x18298c2d8 0x1941b80e4 0x18298c218 0x1829c6064 0x182918b24 0x182917b04 0x1829c6360 0x1829c58f8 0x1829c9188 0x1837b6960 0x10011ec00 0x10011ed00 0x187401404 0x1873ea4e0 0x187400da0 0x187400a2c 0x1873f9f68 0x1873cd18c 0x18766e324 0x1873cb6a0 0x182944240 0x1829434e4 0x182941594 0x18286d2d4 0x18c0836fc 0x187432fac 0x1000f3518 0x194836a08) libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

请记住,每当您遇到此类错误时:“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:尝试为键插入非属性列表对象”。这是因为您可以将图像直接存储到 NSUserDefaults 中,因为它不是键值。

要实现这一点,您必须将 UIImage 转换为 NSData,然后必须通过与键关联来存储图像数据。当你使用检索数据时,你需要将它转换成 UIImage 然后你才能使用它。

示例如下:存储和检索图像。

+(void)saveImageInUserDefaults:(UIImage*)img key:(NSString *)key
{
NSData* pictureData = UIImagePNGRepresentation(img);
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:pictureData];
[[appDelegate userDefaults] setObject:encodedObject forKey:key];
[[appDelegate userDefaults] synchronize];
}

+(UIImage*)getImageFromUserDefaultsWithKey:(NSString *)key
{
NSData *encodedObject = [[appDelegate userDefaults] objectForKey:key];
if (encodedObject)
{
NSData *imgData = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
UIImage *img=[UIImage imageWithData:imgData];
return img;
}
return nil;
}

关于ios - 存储和检索图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140450/

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