UIImageView,从远程 URL 加载 UIImage [英] UIImageView, Load UIImage from remote URL
问题描述
这个问题让我发疯...我有这个字符串 url
:
verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg"我必须在我的 imageView
中加载这张图片.
This problems it's driving me crazy...
I have this string url
:
"verona-api.municipiumstaging.it/system/images/image/image/22/app_1920_1280_4.jpg"
and I have to load this image in my 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)
}
这会抛出异常:
没有这样的文件或目录.
No such file or directory.
但是如果我用浏览器搜索这个url
,我可以正确地看到图像!
But if I search this url
with a browser I can see the image correctly!
推荐答案
您使用了错误的方法来创建 URL.尝试 URLWithString
而不是 fileURLWithPath
.fileURLWithPath
用于从本地文件路径而不是 Internet url 获取图像.
You are using wrong method to create URL. Try URLWithString
instead of fileURLWithPath
. fileURLWithPath
is used to get image from local file path not from 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)
}
这篇关于UIImageView,从远程 URL 加载 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!