Swift - 从 userDefaults 保存和检索图像 [英] Swift - saving and retrieving images from userDefaults

查看:101
本文介绍了Swift - 从 userDefaults 保存和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存从 Parse.com 检索到的图像,如下所示:

 let userImageFile = object["Image"] as PFFileuserImageFile.getDataInBackgroundWithBlock {(imageData: NSData!, 错误: NSError!) ->作废如果错误 == 零 {图像 = UIImage(data:imageData)让 imageToSave:NSData = UIImagePNGRepresentation(image)self.saveImage(intRandomNumb, retImage: imageToSave)}}

saveImage 函数如下所示:

func saveImage(imagepath:Int, retImage:NSData){println("图片正在保存")让默认值 = NSUserDefaults.standardUserDefaults()让 imagePathName = "\(imagepath)"defaults.setObject(retImage, forKey: imagePathName)}

后来,我试图像这样显示这个图像:

 var point =gestureRecognizer.locationInView(self.tv)如果让 indexPath = self.tv.indexPathForRowAtPoint(point){让 data = mainList[indexPath.row] 作为 SecondModel让 fileRef = data.fileReference让 intFileRef = Int(fileRef)println(intFileRef)让默认值 = NSUserDefaults.standardUserDefaults()让 usedKeyName = "\(intFileRef)"if let photo = defaults.objectForKey(usedKeyName) as?用户界面{println("创建的图像")让 photo = defaults.objectForKey(usedKeyName) 作为 UIImagevar imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))imageView.image = 照片self.view.addSubview(imageView)}

并且创建的图像"永远不会被打印,这意味着以某种方式检索不起作用.我不太确定您是否能够像我在这里所做的那样将图像保存到用户默认值,但这是我能想到的最好的方法,而且我找不到任何以前的 Swift 问题.

对如何进行的任何建议将不胜感激.

解决方案:问题是我试图将图像直接加载为 UIImage.我还必须将 NSData 转换为 UIImage,这一切都发生在上面显示的代码的最后一部分.最后我的代码看起来像这样:

 if let photo = defaults.objectForKey("\(intFileRef)") as?NSData {println("创建的图像")let photo = defaults.objectForKey("\(intFileRef)") 作为 NSData让 imageToView:UIImage = UIImage(data: photo)var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))imageView.image = imageToViewself.view.addSubview(imageView)}

我希望这可以帮助其他在类似问题上挣扎的人.

解决方案

Swift 3

嘿,在这里试试这个漂亮的代码:

  • 将您的 UIImage 转换为 Data.

    PNG:

<块引用>

 yourDataImagePNG = UIImagePNGRepresentation(yourUIImageHere)

JPEG:

<块引用>

 yourDataImageJPG = UIImage(data: yourUIImageHere,scale:1.0)

  • 保存在 UserDefaults 中.
<块引用>

 UserDefaults().set(yourDataImagePNG, forKey: "image")

  • 恢复:
<块引用>

 UserDefaults.standard.object(forKey: "image") as!数据

希望能帮到你!

I'm trying to save images retrieved from Parse.com like this:

                    let userImageFile = object["Image"] as PFFile
                    userImageFile.getDataInBackgroundWithBlock {
                        (imageData: NSData!, error: NSError!) -> Void in
                        if error == nil {
                            image = UIImage(data:imageData)
                            let imageToSave:NSData = UIImagePNGRepresentation(image)
                            self.saveImage(intRandomNumb, retImage: imageToSave)
                        }
                    }

where the saveImage-function looks like this:

func saveImage(imagepath:Int, retImage:NSData){

    println("image is being saved")

    let defaults = NSUserDefaults.standardUserDefaults()
    let imagePathName = "\(imagepath)"

    defaults.setObject(retImage, forKey: imagePathName)

}

and later, I'm trying to display this image like this:

        var point = gestureRecognizer.locationInView(self.tv)
        if let indexPath = self.tv.indexPathForRowAtPoint(point)
        {
            let data = mainList[indexPath.row] as SecondModel
            let fileRef = data.fileReference
            let intFileRef = Int(fileRef)
            println(intFileRef)

            let defaults = NSUserDefaults.standardUserDefaults()
            let usedKeyName = "\(intFileRef)"

            if let photo = defaults.objectForKey(usedKeyName) as? UIImage {
                println("Image created")
                let photo = defaults.objectForKey(usedKeyName) as UIImage

                var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
                imageView.image = photo
                self.view.addSubview(imageView)
            }

and the "Image created" never gets printed which means the retrieving somehow doesn't work. I'm not quite sure if you're able to save images to the userdefaults like I've done here, but that was the best I could come up with, and I couldn't find any previous questions like this for Swift.

Any suggestions on how to proceed would be appreciated.

SOLUTION: The problem was that I tried to load the image directly as a UIImage. I also had to convert the NSData to a UIImage, this all happens in the last section of the code displayed above. Finally my code looks like this:

        if let photo = defaults.objectForKey("\(intFileRef)") as? NSData {
            println("Image created")
            let photo = defaults.objectForKey("\(intFileRef)") as NSData
            let imageToView:UIImage = UIImage(data: photo)

            var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
            imageView.image = imageToView
            self.view.addSubview(imageView)
        }

I hope this can help others struggling with something similar to this.

解决方案

Swift 3

Hey, try this beautiful code here:

  • Convert your UIImage to Data.

    PNG:

  yourDataImagePNG = UIImagePNGRepresentation(yourUIImageHere)

JPEG :

 yourDataImageJPG = UIImage(data: yourUIImageHere,scale:1.0)

  • Save in UserDefaults.

    UserDefaults().set(yourDataImagePNG, forKey: "image")

  • Recover from:

    UserDefaults.standard.object(forKey: "image") as! Data

I hope to help!

这篇关于Swift - 从 userDefaults 保存和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆