【问题标题】:How to download image from Url using of NSURlConnection to save in phone cache如何使用 NSURlConnection 从 Url 下载图像以保存在手机缓存中
【发布时间】:2016-11-04 09:41:33
【问题描述】:

我正在使用 NSUrlconnection 调用 api,但现在我也从 api 接收图像,所以我不知道如何加载图像并保存到手机本地内存中,因为这些图像在 app 中进一步使用。请帮我。如何使用和显示本地保存的图像。我正在使用下面的函数来加载图像,但 NSURLSession.sharedSession() 不会进入队列。

 func saveImage(url:NSString,image_name1:NSString)
    {
        NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url as String)!, completionHandler: { (data, response, error) -> Void in

                        if error != nil {
                            print(error)
                            return
                        }
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            let image = UIImage(data: data!)
                            var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                            let documentsDirectory = paths[0]
                            let path = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(image_name1 as String)!.absoluteString
                            print(" file path is \(path)")
                            let data = UIImagePNGRepresentation(image!)
                            data!.writeToFile(path!, atomically: true)

                        })

                    }).resume()



   }

【问题讨论】:

  • NSURLConnection 已弃用,您应该使用 NSURLSession
  • 兄弟,但它在我的代码中工作

标签: ios swift xcode nsurlconnection


【解决方案1】:

保存

var image = ....  // However you create/get a UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)

现金

https://github.com/onevcat/Kingfisher

pod '翠鸟'

let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)

这是自动现金图片

【讨论】:

  • 兄弟,我的要求是先存储本地内存,然后使用这些图像
  • var image = .... // 但是你创建/获取一个 UIImage let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let destinationPath = documentsPath.stringByAppendingPathComponent(" filename.jpg") UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)
  • 兄弟使用nsurlconnection如何加载图片
【解决方案2】:

试试这个:

let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
imageView.image = UIImage(data: data!)

发件人:https://stackoverflow.com/a/27517280/3901620

【讨论】:

  • 让数据 = 试试? NSData(contentsOfURL:(NSURL(string: url as String))!) let image = UIImage(data: data!!) var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = paths[0] let path = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(image_name1 as String)!.absoluteString print("file path is (path)") let data1 = UIImagePNGRepresentation(image!) let result = data1!.writeToFile(path!, atomically: true) print("result is (result)") 它显示为假
  • 你得到数据中的字节了吗?
猜你喜欢
  • 2020-05-25
  • 2019-06-09
  • 2019-02-05
  • 1970-01-01
  • 2017-12-24
  • 2020-03-03
  • 2012-01-23
  • 1970-01-01
  • 2019-05-26
相关资源
最近更新 更多