我想要显示imageview的背景颜色,直到下载正在进行,如果下载失败或图像不可用,那么我想显示一个占位符图像。我怎样才能做到这一点?
主要目标是稍后不在加载过程中设置图像。
由于
答案 0 :(得分:5)
来自SDWebImage Documentation:
使用块
使用块,您可以收到有关图像下载进度的通知 每当图像检索成功完成时:
// Here we use the new provided sd_setImageWithURL: method to load the web image
对于Swift:
cell.imageView.sd_setImageWithURL(url, placeholderImage:nil, completed: { (image, error, cacheType, url) -> Void in
if (error) {
// set the placeholder image here
} else {
// success ... use the image
}
})
for Objective-C
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (error) {
self.imageView.image = [UIImage imageNamed:@"placeHolderImage"];
}
}];
答案 1 :(得分:4)
Swift 3的解决方案:
cell.imageView?.sd_setImage(with: url) { (image, error, cache, urls) in
if (error != nil) {
cell.imageView.image = UIImage(named: "ico_placeholder")
} else {
cell.imageView.image = image
}
}
目标C的解决方案:
[cell.imageView sd_setImageWithURL:url
placeholderImage:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (error) {
self.imageView.image = [UIImage imageNamed:@"ico_placeholder"];
} else {
self.imageView.image = image;
}
}];
希望你们觉得这很有用。
答案 2 :(得分:1)
您可以将nil
传递给占位符属性并自行处理完成块。
像这样:
[self.imageView sd_setImageWithURL:self.imageURL
placeholderImage:nil
options:nil
progress:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if(!error) {
self.imageView.image = image;
} else {
self.imageView.image = [UIImage imageNamed:@"placeHolder"];
}
}];
我没试过。试试看,告诉我这是否适合你。
答案 3 :(得分:1)
在这里,这是用于分隔加载/错误图像的扩展。 loadingImage将是SDWebImage称为placeholderImage的东西。加载错误时,errorImage将是真正的占位符。
import Foundation
import SDWebImage
extension UIImageView {
/// Separated loader and error images, short version.
func zap_setImage(with url: URL?, loadingImage: UIImage?, errorImage: UIImage) {
self.zap_setImage(with: url, loadingImage: loadingImage, errorImage: errorImage, completed: nil)
}
/// Separated loader and error images, long version.
func zap_setImage(with url: URL?, loadingImage: UIImage?, errorImage: UIImage, completed: SDExternalCompletionBlock?) {
self.sd_setImage(with: url, placeholderImage: loadingImage, options: []) { [weak self] (uiimage: UIImage?, error: Error?, sdimagecachetype: SDImageCacheType, usedurl: URL?) in
//print("ISSUE1688 uiimage: \(String(describing: uiimage)), error: \(String(describing: error))")
let setErrorImage: Bool
switch error {
case .none:
// No error?
if uiimage == nil {
// No error, but no image, so use error image
setErrorImage = true
}
else {
setErrorImage = false
}
break
case .some(_):
// case .some(let errorvalue):
// Error?
//print("ISSUE1688 errorvalue: \(errorvalue)")
setErrorImage = true
break
}
//print("ISSUE1688 setErrorImage: \(setErrorImage)")
if setErrorImage {
self?.image = errorImage
}
if let c = completed {
c(uiimage, error, sdimagecachetype, usedurl)
}
}
}
}
答案 4 :(得分:0)
您可以使用它。
cell.img_UserProfile.sd_setImage(with: URL.init(string: "\(BaseURL_Image)\(image)"), placeholderImage: #imageLiteral(resourceName: "Group 1131"), options: .highPriority, context: [:])
答案 5 :(得分:0)
使用'SDWebImage','〜> 4.0' 使用此imageview扩展名
top: 0