使用 activityIndi​​cator 作为图像占位符

Use activityIndicator as image placeholder

我在我的项目中使用 UIImageView+AFNetworking.swift,并使用占位符将图像加载到 imageView,我使用此代码:

imageCell!.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "PlaceholderIMG"))

如何将 PlaceholderIMG 替换为 activityIndicator

使用来自 UIImageView+setImageWithURLRequestsetImageWithURLRequest 而不是您的解决方案。所以这是一个粗略的例子:

// create a UIActivityIndicatorView with the 
let ai = UIActivityIndicatorView(frame: cell.imageView.frame)
// Add the UIActivityIndicatorView as a subview on the cell
cell.addSubview(ai)
// Start the UIActivityIndicatorView animating
ai.startAnimating()
// Load the remote image with this different API, and send nil as the placeholder
cell.imageView?.setImageWithURLRequest(NSURLRequest(URL: NSURL(string: post.image)),
    placeholderImage: nil,
    success: { (request, response, image) -> Void in
        // on completion, stop it and remove it
        ai.stopAnimating()
        ai.removeFromSuperview()
        cell.imageView?.image = image
    },
    failure: { (request, response, error) -> Void in
        // on completion, stop it and remove it
        ai.stopAnimating()
        ai.removeFromSuperview()
})