【问题标题】:How to make images appear in the view with its labels in UICollectionView cells using Alamofire?如何使用 Alamofire 在 UICollectionView 单元格中使用标签显示图像?
【发布时间】:2017-02-08 08:29:11
【问题描述】:

我正在使用 UICollectionView 制作像 AppStore 这样的应用程序,所以当从 url 下载数据时

import Alamofire
import AlamofireImage

class AppCategory: NSObject {
var id: NSNumber?
var title: String?
var apps: [App]?
var type: NSNumber?

static func fetchFeaturedApps(completionHandler: @escaping ([AppCategory]) -> ()) {

let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]
var appCategories = [AppCategory]()
             let sections = OFFERIM["sections"] as! [Dictionary<String, AnyObject>]
            for i in 0..<sections.count {
                var appCategory = AppCategory()
                var apps = [App]()

                let sectionTitle = sections[i]["title"] as! String
                appCategory.title = sectionTitle
                let sectionType = sections[i]["type"] as! NSNumber
                appCategory.type = sectionType
                let sectionId = sections[i]["id"] as! NSNumber
                appCategory.id = sectionId
                // for Apps inside each section
                let lists = sections[i]["lists"] as! [String: AnyObject]
                 for j in 0..<lists.count{
                    let app = App()

                    let id = lists[j]["id"] as! NSNumber
                    app.id = id

                    let thumb = lists[j]["thumb"]! as! String

                    Alamofire.request(thumb).responseImage { response in

                        if let image = response.result.value {
                            app.image = image
                            print("image downloaded: \(image)")
                        }
                    }
                    let title = lists[j]["title"] as! String
                    app.title = title

                    apps.append(app)
                }
                appCategory.apps = apps
                appCategories.append(appCategory)
            }
            DispatchQueue.main.async(execute: {
                completionHandler(appCategories)
            })
        }catch{
            print("JSON Processing Failed")
        }
        }.resume()
}
}

我将这些类作为模型

class App: NSObject {
var id: NSNumber?
var title: String?
var image: UIImage?

}

我在 viewController 的 viewDidLoad 中调用 featuredApps 发生的情况是当视图加载标签出现但图像不在集合视图中时。

我需要滚动每一行以显示图像。

我不知道问题在哪里???

【问题讨论】:

  • 你在调用 collectionView.reloadData() 吗?如果您在滚动后看到它们,这可能是问题所在。
  • @JustinM 绝对是我在调用这个静态函数之后调用的
  • 对图像的 alamofire 请求是在它自己的闭包中,所以你很可能在下载图像之前调用你的 completionHandler。由于您在滚动时看到图像,因此与获取图像和重新加载集合视图的时间有关
  • @JustinM 那么你的建议是什么?
  • 首先要计算出您希望下载的图像总数,然后从下载图像函数内部调用您的完成处理程序。如果您可以提前在后台线程上下载图像,您可以缓存它们并使其更具响应性。您可能需要处理这些问题才能了解最有效的方法。

标签: ios swift3 uicollectionview alamofireimage


【解决方案1】:

保留 url 并将 url 设置为使用 AlamofireImage 的图像视图。

imageView.af_setImage(withURL: url)

【讨论】:

  • 同样的结果!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 2016-06-23
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多