xcode 显示带有来自 URL 的图像的 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37872136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:55:38  来源:igfitidea点击:

Display UIImage with an image from a URL

iosxcodeswiftuiimage

提问by Rorschach

I am trying to display a list of items. My end game is displaying a grid of images from an array of Strings that are URLs and file names on my IOS app.

我正在尝试显示项目列表。我的最终游戏是在我的 IOS 应用程序上显示来自一系列字符串的图像网格,这些字符串是 URL 和文件名。

I have a CollectionView that is displaying a grid of images via an array of String filenames, and that works:

我有一个 CollectionView,它通过一个字符串文件名数组显示图像网格,并且有效:

Array:

大批:

var tableImages: [String] = ["car.jpg", "bus.jpg", "plane.jpg"]

code that works:

有效的代码:

cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

My next step is figuring out the code for displaying a grid of images via an array of String urls. And that is my question. How do I display an image from a url?

我的下一步是找出通过字符串 url 数组显示图像网格的代码。这就是我的问题。如何显示来自 url 的图像?

Array:

大批:

var tableImages: [String] = ["http://www.hotelstmarie.com/wp-content/uploads/2014/12/HSM_Guest-Rooms_1500x750-06-02-2015.jpg", "http://media-cdn.tripadvisor.com/media/photo-s/03/4f/b0/58/aviva-by-kameel.jpg"]

code that works:

有效的代码:

?

Notes:

笔记:

I have checked other stackoverflow questions on the matter but they are from years ago and the move to swift has made their answers out of date.

我已经检查了有关此事的其他 stackoverflow 问题,但它们是多年前的问题,而转向 swift 已经使他们的答案过时了。

After I figure out how to display url images I will just make some if statements to recognize url or filename to make it to my endgame, but that is not part of this question.

在我弄清楚如何显示 url 图像后,我将做一些 if 语句来识别 url 或文件名,以使其进入我的最后阶段,但这不是这个问题的一部分。

EDIT 1

编辑 1

Code around my code above:

围绕我上面的代码编写代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]
    cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
    return cell
}

EDIT 2

编辑 2

Using Alamofire:

使用 Alamofire:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]
    let imageUrl = NSURL(string: tableImages[indexPath.row])
    cell.imgCell.af_setImageWithUrl(imageUrl, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.2))
    return cell
}

Gives this error:

给出这个错误:

Value of type 'UIImageView' has no member 'af_setImageWithUrl'

回答by Jayesh Miruliya

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]

    if let url = NSURL(string: tableImages[indexPath.row])
    {
        if let data = NSData(contentsOfURL: url)
        {
            cell.imgCell.image = UIImage(data: data)
        }
    }

    return cell
}

回答by Daven

You would write:

你会写:

if let url = NSURL(string: tableImages[indexPath.row]){
    let data = NSData(contentsOfURL: url)
    let image = UIImage(data: data!)
    cell.imgCell.image = image
}

Although I recommend using an existing Swift library (like Kingfisher) that handles thread management and caching for you, so your app doesn't run out of memory or freeze while it's downloading the images. I currently use SDWebImage.

尽管我建议使用现有的 Swift 库(如Kingfisher)来为您处理线程管理和缓存,因此您的应用程序在下载图像时不会耗尽内存或冻结。我目前使用SDWebImage

回答by EricDS

The way you are setting your image here:

您在此处设置图像的方式:

cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

Only works for local images in your project, so your all will try to look for an image named the same as your url locally.

仅适用于您项目中的本地图像,因此您将尝试在本地查找与您的 url 名称相同的图像。

While you could load the image manually, the best alternative is to use an image loading library to deal with common issues, like avoiding old images to load on top of your new ones when your view gets recycled.

虽然您可以手动加载图像,但最好的替代方法是使用图像加载库来处理常见问题,例如避免在视图被回收时将旧图像加载到新图像之上。

In swift i have used AlamofireImagelike so:

在 swift 我已经这样使用了AlamofireImage

let imageUrl = NSURL(string: tableImages[indexPath.row])
cell.imgCell.af_setImageWithURL(imageUrl, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.2))

as you can see, this library uses swift's extensionswhich greatly simplifies it's api.

正如你所看到的,这个库使用了 swift 的extensions,这大大简化了它的 api。

回答by skJosh

I have used Swift version of UIImageView+AFNetworking from AFNetworking library which you can get from github. Very easy to use. Just drop the file in your project and you can start using it as following:

我使用了 AFNetworking 库中 UIImageView+AFNetworking 的 Swift 版本,您可以从github获得。非常容易使用。只需将文件放入您的项目中,您就可以开始使用它,如下所示:

   cell.imageView.setImageWithUrl(imageUrl!,placeHolderImage: UIImage(named: "placeholder"))