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

时间:2015-12-08 17:57:20

标签: ios swift afnetworking afnetworking-2

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

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

如何用PlaceholderIMG代替activityIndicator

1 个答案:

答案 0 :(得分:1)

使用UIImageView + AFNetworking.swift中的setImageWithURLRequest而不是您的解决方案。所以这是一个粗略的例子:

// 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()
})