图像路径未在swift3中转换为url [英] Image path not convert to url in swift3

查看:94
本文介绍了图像路径未在swift3中转换为url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有回复,我想从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

推荐答案

该URL不是本地文件路径URL,也不是我的浏览器附带的有效URL.

That URL is not a local file path URL, nor a valid URL accoridng to my browser.

上面提供的URL在浏览器中返回服务器错误,并且不返回图像.查看屏幕截图

The URL you have provided above returns a server error in the browser and does not return an image. See screenshot

您需要确保图像可访问,并且URL实际上首先返回图像响应.然后,您将需要下载图像.不知道您是否正在使用任何库,因此我将发布一个没有示例的示例.

You would need to ensure that the image is accessible and the URL actually returns an image response firstly. Then you would need to download the image. Not sure if you are using any libraries or not so I will post an example without.

//: 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

更新:

这篇关于图像路径未在swift3中转换为url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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