ios - Alamofire 图片 : Placeholder image is not set for af_setImageWithURL

标签 ios swift cocoa-touch alamofire alamofireimage

我正在使用 af_setImageWithURL Alamofire API 来获取表格单元格图像并传递默认占位符图像:

let defaultImage = UIImage(named:"myImage")
cell.imageView.af_setImageWithURL(imageURL, placeholderImage:defaultImage, imageTransition: .CrossDissolve(0.1))

问题: 当下载路径中不存在我的图像时,单元格也不会显示默认图像。当我向上和向下滚动表格(清除并重新使用同一个单元格)时,它就会出现。

编辑:

注意到这是在单元格出队后发生的,我先重置图像,然后调用 Alamofire。如果我在下面注释掉,它工作正常,但如果图像 URL 和默认图像都不可用,我就会遇到显示旧产品图像的风险。

cell.imageView.image = nil

有哪位遇到过类似问题,请指教。

最佳答案

您需要确保您取消了您的请求,并且 nilprepareForReuse 中的图像。否则你会遇到一些不同的问题。这是 AFI 中 iOS 示例 中的 ImageCell 类。

class ImageCell: UICollectionViewCell {
    class var ReuseIdentifier: String { return "org.alamofire.identifier.\(type(of: self))" }
    let imageView: UIImageView

    // MARK: - Initialization

    override init(frame: CGRect) {
        imageView = {
            let imageView = UIImageView(frame: frame)

            imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            imageView.contentMode = .center
            imageView.clipsToBounds = true

            return imageView
        }()

        super.init(frame: frame)

        contentView.addSubview(imageView)

        imageView.frame = contentView.bounds
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Lifecycle Methods

    func configureCell(with URLString: String, placeholderImage: UIImage) {
        let size = imageView.frame.size

        imageView.af_setImage(
            withURL: URL(string: URLString)!,
            placeholderImage: placeholderImage,
            filter: AspectScaledToFillSizeWithRoundedCornersFilter(size: size, radius: 20.0),
            imageTransition: .crossDissolve(0.2)
        )
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        imageView.af_cancelImageRequest()
        imageView.layer.removeAllAnimations()
        imageView.image = nil
    }
}

干杯。 🍻

关于ios - Alamofire 图片 : Placeholder image is not set for af_setImageWithURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39097877/

相关文章:

ios - 错误 : registering a custom tableView cell in swift with an xib file

iphone - 处理 CGMutablePath 时内存泄漏

iphone - 为什么要使用像 CGRectMake 这样的函数?

objective-c - 停止执行并获得用户在 iPad 应用程序上的选择

ios - 停止重复本地通知 iOS

ios - 导出通用分发框架

ios - 线程 1 : Signal sigabrt. 不知道如何解决这个问题

objective-c - 为什么在 iPhone 中从服务器发送和接收 JSON 数据这么慢?

ios - 滚动旋转时 UICollectionView 崩溃(索引路径处补充项目的布局属性已更改但未失效..)

swift - 如何显示和隐藏标签?