问题描述
我有一个用户拍照的应用程序,一旦拍照,它就在某个 UICollectionView
中。我一直收到这个错误:
I have an app where the user takes a picture and once the picture is taken it is in a certain UICollectionView
. I keep getting this error:
"cannot invoke 'setObject' with an argument list of type '(UIImage. type, forKey:[UIImage])'"
我在这里搜索了文档和帖子,但没有任何帮助。
这是我的代码。 (注意:我只在一个案例中做过,原因很明显)
I've searched through documentation and posts on here, but nothing helps.This is my code. (note: I only did it in one case, for obvious reasons)
//userdefualts
let defualts = NSUserDefaults.standardUserDefaults()
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)
switch currentClothesSelection!{
case "1":
shirts.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
//this is where the error occurs
defualts.setObject(UIImage.self, forKey: array1)
case "2" :
pants.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
case "3" :
shorts.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
case "4" :
jackets.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
case "5" :
shoes.append(info[UIImagePickerControllerOriginalImage] as! UIImage)
default:
break
}
请帮忙。在此先感谢。
推荐答案
编辑/更新:
Xcode 8 beta•Swift 3
在NSUserDefaults存储图像不是一个好主意,但如果你真的想这样做,你需要存储是as NSData。
It is not a good idea to store images at NSUserDefaults but if you really want to do it you need to store is as NSData.
我建议将它本地保存到Documents文件夹中的磁盘。如果你真的想把它保存到NSUserDefaults,我建议只使用小图片并自行承担风险:)你可以这样做:
I recommend saving it locally to disk at the Documents Folder. If you really want to save it to NSUserDefaults, I recommend using just for small images and do it at your own risk :) You can do as follow:
extension UserDefaults {
func set(image: UIImage?, forKey key: String) {
guard let image = image else {
set(nil, forKey: key)
return
}
set(UIImageJPEGRepresentation(image, 1.0), forKey: key)
}
func image(forKey key:String) -> UIImage? {
guard let data = data(forKey: key), image = UIImage(data: data )
else { return nil }
return image
}
func set(imageArray value: [UIImage]?, forKey key: String) {
guard let value = value else {
set(nil, forKey: key)
return
}
set(NSKeyedArchiver.archivedData(withRootObject: value), forKey: key)
}
func imageArray(forKey key:String) -> [UIImage]? {
guard let data = data(forKey: key),
imageArray = NSKeyedUnarchiver.unarchiveObject(with: data) as? [UIImage]
else { return nil }
return imageArray
}
}
测试:
let myImage = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
UserDefaults.standard().set(image: myImage, forKey: "anyKey")
if let myLoadedImage = UserDefaults.standard().image(forKey:"anyKey") {
print(myLoadedImage.size) // "(719.0, 808.0)"
}
let myImagesArray = [myImage, myImage]
UserDefaults.standard().set(imageArray: myImagesArray, forKey: "imageArrayKey")
if let myLoadedImages = UserDefaults.standard().imageArray(forKey:"imageArrayKey") {
print(myLoadedImages.count) // 2
}
这篇关于使用nsuserdefaults保存图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!