斯威夫特:如何了解下载的图片为空?

时间:2019-05-16 07:58:31

标签: ios swift image-processing

我用翠鸟下载图像,但有时图像存在,但为空。

示例: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
                        }
                    }
                }
            })
        }
    }

2 个答案:

答案 0 :(得分:1)

请参阅以下代码,从服务器获取图像后检查图像大小。如果文件大小为0,则图像为空。

 temp_cost = sess.run([optimizer , cost] , feed_dict= {X:minibatch_X , Y : minibatch_Y })
        ### END CODE HERE ###  

答案 1 :(得分:0)

尝试

 print(imageUrl.absoluteString) // check image file name at the end of url