【问题标题】:Image path not convert to url in swift3swift3中的图像路径未转换为url
【发布时间】:2017-11-06 04:50:19
【问题描述】:

我有回复我想从image_path下载图片

let fileUrl = NSURL(fileURLWithPath: (posts.value(forKey: "image_path") as! [String])[indexPath.row])
        print(fileUrl as Any) // here i can get path

        if FileManager.default.fileExists(atPath: (fileUrl)// nil value {
            let url = NSURL(string: (posts.value(forKey: "image_path") as! [String])[indexPath.row]) // Here url found nil
            let data = NSData(contentsOf: url! as URL)
            cell.LocationImage?.image = UIImage(data: data! as Data)
        }

更新:

S9.png

【问题讨论】:

  • 用 afnetworking 3.0 stackoverflow.com/questions/36007238/… 查看我的答案,或者您可以使用 sd webimage 下载图像。如果您的网址在服务器外部
  • @ViralRathod 您的 image_path 是帖子响应中的 URL??
  • @TusharSharma 帖子是我对肥皂的回应,'posts.value(forKey: "image_path") as! [字符串])[indexPath.row]'
  • @ViralRathod 我的意思是键“image_path”的值是什么。它是 URL 吗??

标签: ios iphone swift swift3 uiimageview


【解决方案1】:

该 URL 不是本地文件路径 URL,也不是根据我的浏览器的有效 URL。

您在上面提供的 URL 在浏览器中返回服务器错误并且不返回图像。看截图

您需要确保图像可访问,并且 URL 首先实际上返回了图像响应。然后你需要下载图像。不确定您是否使用任何库,所以我将发布一个没有的示例。

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground
import PlaygroundSupport

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

// random image from images.google.com
let urlString = "https://files.allaboutbirds.net/wp-content/uploads/2015/06/prow-featured-240x135.jpg"

let url = URL(string: urlString)
let session = URLSession.shared

let task = session.dataTask(with: url!) { data, response, error in
    guard error == nil else {
        print("[ERROR] - Failed to download image")
        return
    }

    if let data = data {
        let image = UIImage(data: data)
        DispatchQueue.main.async {
            imageView.image = image
        }
    }
}

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.addSubview(imageView)

task.resume()
PlaygroundPage.current.liveView = view

更新:

【讨论】:

  • 这是我的回复 URL "sf.iipl.info/ImageCSharp.aspx?Location=C:\\infinityhost\\demo11.iipl.info\\data\\app\\user_location_photo\\8000034939_Resize\\&FileName=1_35f28e55-ca31-4918- 8b41-9eb7f3070ace"
  • 在浏览器中试一试...是显示图像还是服务器错误?
  • 当你在浏览器中粘贴时直接下载图片,我可以在预览中打开该图片
  • 查看我的更新答案。该图像位于远程服务器上,据我所知,文件 URL 应该用于本地资源。确保 URL 有效并尝试我上面的代码。看看有没有效果
  • 请参阅stackoverflow.com/questions/44327312/…stackoverflow.com/questions/44363372/…,如果您能提供更多帮助,请参阅
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2018-06-14
  • 2017-05-11
相关资源
最近更新 更多