【问题标题】:Trouble retrieving images with Alamofire, some images load, some don't (SWIFT)使用 Alamofire 检索图像时遇到问题,有些图像加载,有些则没有 (SWIFT)
【发布时间】:2016-07-19 17:46:58
【问题描述】:

所以我有点卡在这里。我有一个移动应用程序,它有一个使用 TableView 的“提要”,它填充了来自 firebase 的图片、描述和喜欢。我将 ImageShack 用于图像上传 API,并从那里抓取 imgLinks(使用 alamofire)。图片正在上传,在我的 ImageShack 面板上,图片看起来很棒。所以我知道这不是firebase,也不是imageshack的错,但大多数图片甚至都无法加载,它们只是白框,我可以确认imageUrl!= nil,所以这绝对是Alamofire的请求问题。
这是我的 Alamofire GET 请求代码
如果 post.imageUrl != nil {

        if img != nil {
            self.deckImg.image = img
        } else {

            request = Alamofire.request(.GET, post.imageUrl!).validate(contentType:     ["image/*"]).response(completionHandler: { request, response, data, err     in
                if err == nil {
                    let img = UIImage(data: data!)!
                    self.deckImg.image = img
                    FeedController.imageCache.setObject(img, forKey: self.post.imageUrl!)
                } else {
                    print(err.debugDescription)
                }
            })
        }
    } else {
        self.deckImg.hidden = true

    }

另外,我可以确认它没有隐藏图像,因为当 self.deckImg.hidden = true 时,我调整了行的高度,而且我还可以看到它从未被调用,因为它甚至从未达到那个断点。所以这只是与 Alamofire 的获取请求有关.. 它是什么:(

【问题讨论】:

  • 首先,我建议您考虑使用 [AlamofireImage][1],它在 Alamofire 之上提供图像加载基础设施,包括它自己的缓存和图像解码。除此之外,我们确实需要更多信息来帮助您。你拿回数据了吗?变成了合适的UIImage? [1]:github.com/alamofire/alamofireimage

标签: iphone swift firebase alamofire imageshack


【解决方案1】:

我想我知道问题出在哪里,因为我连续几天都被困住了。 我认为您使用的是自定义 tableViewCell VC,并且在绑定数据的函数中您放置了如下内容:

self.deckImg.image = nil

然后检查是否有缓存的图片,如果没有就用alamofire下载 检查 post.imageUrl != nil {} 后,您必须取消隐藏 self.deckImg:

if post.imageURL != nil
{
    self.deckImg.hidden = false

    if img != nil 
    {
        self.deckImg.image = img
    } 
    else 
    {
        request = Alamofire.request(.GET, post.imageUrl!).validate(contentType:     ["image/*"]).response(completionHandler: { request, response, data, err     in
            if err == nil {
                let img = UIImage(data: data!)!
                self.deckImg.image = img
                FeedController.imageCache.setObject(img, forKey: self.post.imageUrl!)
            } else {
                print(err.debugDescription)
            }
        })
    }
}
 else
{
    self.deckImg.hidden = true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    相关资源
    最近更新 更多