【问题标题】:UIImageView, Load UIImage from remote URLUIImageView,从远程 URL 加载 UIImage
【发布时间】:2017-10-31 08:24:02
【问题描述】:

这个问题快把我逼疯了... 我有这个字符串url:
verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg” 我必须在我的imageView 中加载这张图片。

这是我的代码:

do {
    let url = URL(fileURLWithPath: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg")
    let data = try Data(contentsOf: url)
    self.imageView.image = UIImage(data: data)
}
catch{
    print(error)
}

这会抛出异常:

没有这样的文件或目录。

但如果我用浏览器搜索这个url,我可以正确看到图像!

【问题讨论】:

标签: ios swift url uiimageview uiimage


【解决方案1】:

您使用错误的方法创建 URL。尝试URLWithString 而不是fileURLWithPathfileURLWithPath 用于从本地文件路径而不是从 Internet url 获取图像。

do {
    let url = URL(string: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg")
    let data = try Data(contentsOf: url)
    self.imageView.image = UIImage(data: data)
}
catch{
    print(error)
}

【讨论】:

【解决方案2】:

fileURLWithPath 方法从文件系统打开文件。文件地址以file:// 开头。您可以打印 url 字符串。

来自 Apple 关于 + (NSURL *)fileURLWithPath:(NSString *)path; 的文档

NSURL 对象将代表的路径。路径应该是有效的 系统路径,不能为空路径。如果路径以 波浪号,必须先用 stringByExpandingTildeInPath 展开。如果 path 是相对路径,它被视为相对于 当前工作目录。

以下是几种可能的解决方案之一:

let imageName = "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg"

func loadImage(with address: String) {

    // Perform on background thread
    DispatchQueue.global().async {

        // Create url from string address
        guard let url = URL(string: address) else {
            return
        }

        // Create data from url (You can handle exeption with try-catch)
        guard let data = try? Data(contentsOf: url) else {
            return
        }

        // Create image from data
        guard let image = UIImage(data: data) else {
            return
        }

        // Perform on UI thread
        DispatchQueue.main.async {
            let imageView = UIImageView(image: image)
            /* Do some stuff with your imageView */
        }
    }
}

loadImage(with: imageName)

最好的做法是发送一个完成处理程序以在主线程上执行到loadImage(with:)

【讨论】:

  • 我认为在数据中简单地公开 url 是一种不好的做法,对吧?
【解决方案3】:

这里的url不是本地系统的,而是服务器的。

  let url = URL(fileURLWithPath: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg")

这里创建的 url 是设备本地的文件。 像这样创建网址:-

  url = URL(string: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg") 

【讨论】:

    【解决方案4】:

    使用下面的代码 sn-p 将图像加载到 imageview 中

     func imageDownloading() {
    
        DispatchQueue.global().async {
    
            let url = URL(string: "http://verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg")!
    
            do {
    
                let data = try Data(contentsOf: url)
    
                DispatchQueue.main.async {
    
                self.imageView.image = UIImage(data: data)
    
                }
    
            } catch {
                print(error.localizedDescription)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2011-01-22
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多