我正在用翠鸟下载图片,但有时图片存在,但却是空的。
示例:Sample Image如何检测此类图像?
我的下载方法:
func downloadFrom(link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit, rounded: Bool = false, loader: IndicatorType = .none) {
self.kf.indicatorType = loader
guard let url = URL(string: link) else {
return
}
let img = self.image
if(rounded) {
let roundedProcessor = RoundCornerImageProcessor(cornerRadius: 100, targetSize: CGSize(width: 200, height: 200), roundingCorners: .all, backgroundColor: nil)
self.kf.setImage(with: url, placeholder: img, options: [.processor(roundedProcessor)], completionHandler: {
(image, error, cacheType, imageUrl) in
if (error == nil) {
DispatchQueue.main.async() { () -> Void in
if (self.contentMode != UIView.ContentMode.scaleAspectFit) {
self.contentMode = mode
}
}
}
})
} else {
self.kf.setImage(with: url, placeholder: img, completionHandler: {
(image, error, cacheType, imageUrl) in
if (error == nil) {
DispatchQueue.main.async() { () -> Void in
if (self.contentMode != UIView.ContentMode.scaleAspectFit) {
self.contentMode = mode
}
}
}
})
}
}
发布于 2019-05-16 08:25:43
查看以下代码,在从服务器获取镜像后检查镜像大小。如果文件大小为0,则图像为空。
self.kf.setImage(with: url, placeholder: img, options: [.processor(roundedProcessor)], completionHandler: {
(image, error, cacheType, imageUrl) in
if let imageData = UIImagePNGRepresentation(image) {
let bytes = imageData.count
if bytes > 0 {
//getting image successfully
}else{
//Image size is 0 (image is empty)
}
}else{
//Not getting image
}
})
https://stackoverflow.com/questions/56163643
复制相似问题