在 tableview 中显示下载的图像 [英] display downloaded images in tableview
问题描述
我正在 Swift 2.0 中制作一个应用程序,我在原型单元格中使用带有图像视图的表格视图.该应用程序从 facebook 服务器下载了一些图像,我想显示它们.这些图像都是从不同的 url 下载的,所以我正在使用一个程序来循环它们并下载,就像我使用名称从互联网上获取它们并显示一样,但我有点卡住了..
I'm making an app in Swift 2.0 and I'm using a table view with an image view in the prototype cell. The app downloads some images from the facebook server and I want to display them. The images are all downloaded from a different url, so I am using a program to loop trough them and download, just like I did with the names to get them from the internet and display, but I'm a little bit stuck..
func fetchPicture(identifier: String, completion: (image: UIImage) -> Void)
{
let url2 = NSURL (string: "http://graph.facebook.com/" + identifier + "/picture?type=normal")
let urlRequest = NSURLRequest(URL: url2!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) {
(response, data, error) -> Void in
if error != nil
{
print(error)
}
else
{
if let pf = UIImage(data: data!)
{
dispatch_async(dispatch_get_main_queue())
{
completion(image: pf)
self.tableView.reloadData()
}
}
}
}}
var images = [UIImage]()
let queue2 = dispatch_queue_create("images", DISPATCH_QUEUE_SERIAL)
dispatch_apply(newArray.count, queue2) { index in
let identifier = newArray[index]
fetchPicture(identifier) {
image in dispatch_async(queue2)
{
}
}
}
我将单元格中的图像视图设置为等于变量图像",因此基本上我需要执行类似 self.image = pf 的操作,但每个用户都需要不同.对于名称,我使用数组进行了此操作,但这不适用于我假设的图像..
I set the imageview in the cell equal to the variable 'image' so basically I will need to do something like self.image = pf but it needs to be different for each user. With names, I did this with an array, but this isn't working with images I assume..
帮助真的很感激伙计们!谢谢!
Help is really appreciated guys! Thanks!
推荐答案
您通常希望使用专门处理此类问题的框架.我不推荐使用 SDWebImage,它已经过时(没有 NSURLSession),有很多未解决的问题并且不能很好地与 Swift 一起使用(没有可空性注释).
You would generally want to use a framework that specializes on that kind of stuff. I would not recommend using SDWebImage, it's outdated (no NSURLSession), has a lot of open issues and doesn't work well with Swift (no nullability annotations).
看看 iOS 平台上最新的两个库:
Take a look at those two libraries that are up to date with iOS platform:
- DFImageManager - 用 Objective-C 编写的高级框架,但具有可空性注释(适用于 Swift).这是一个事情列表,让它变得更好,比 SDWebImage.披露是我写的,意见可能有偏见.
- Kingfisher - 用 Swift 编写的轻量级库.与 SDWebImage 类似,但功能比 SDWebImage 和 DFImageManager 少得多.
- DFImageManager - advanced framework written in Objective-C but featuring nullability annotations (works great with Swift). Here's a list of things that make it better, than SDWebImage. Disclosure it's written by me, opinion might be biased.
- Kingfisher - lightweight library written in Swift. Similar to SDWebImage, but has much less features that SDWebImage and DFImageManager.
这篇关于在 tableview 中显示下载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!