仅使用Kingfisher库获取UIImage

时间:2016-02-04 15:10:56

标签: ios swift2 uiimage kingfisher

我只需要获取UIImage,而不是使用Kingfisher library加载普通的UIImageView

为了实现它,我使用UIImageView实现了一种解决方法:

if(answer)
   answers[questionindex] = answer;
else
   answers[questionindex] = '';

这段代码完美无缺,但我希望以更干净的方式完成。 这个库中有一个方法只能获得UIImage吗?异步和缓存

7 个答案:

答案 0 :(得分:49)

您可以使用retrieveImage(with:options:progressBlock: completionHandler:)的{​​{1}}方法。

也许是这样的:

KingfisherManager

答案 1 :(得分:10)

使用新的Swift 3版本,您可以使用ImageDownloader:

ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
    (image, error, url, data) in
    print("Downloaded Image: \(image)")
}

ImageDownloader是翠鸟的一部分,所以不要忘记import Kingfisher

答案 2 :(得分:9)

这是在kingFisher 5中下载图像的最新语法(在swift 4.2中进行了测试)

func downloadImage(`with` urlString : String){
    guard let url = URL.init(string: urlString) else {
        return
    }
    let resource = ImageResource(downloadURL: url)

    KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
        switch result {
        case .success(let value):
            print("Image: \(value.image). Got from: \(value.cacheType)")
        case .failure(let error):
            print("Error: \(error)")
        }
    }
}

如何调用上述功能

self.downloadImage(with: imageURL) //replace with your image url

答案 3 :(得分:3)

迅速5:

我将在 @Hardik Thakkar 答案上完成以添加更改,以便您可以使用闭包来返回图像,这可能对某些人有帮助:

func downloadImage(with urlString : String , imageCompletionHandler: @escaping (UIImage?) -> Void){
        guard let url = URL.init(string: urlString) else {
            return  imageCompletionHandler(nil)
        }
        let resource = ImageResource(downloadURL: url)
        
        KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
            switch result {
            case .success(let value):
                imageCompletionHandler(value.image)
            case .failure:
                imageCompletionHandler(nil)
            }
        }
    }

通话方式:

 downloadImage(with :yourUrl){image in
     guard let image  = image else { return}
      // do what you need with the returned image.
 }

答案 4 :(得分:2)

翠鸟5中的另一种方法:

KingfisherManager.shared.retrieveImage(with: url) { result in
    let image = try? result.get().image
    if let image = image {
        ...
    }
}

答案 5 :(得分:0)

雨燕5

let url = URL(string: USER_IMAGE_PATH + "\(arrGuserpic[index])")

cell.imgv.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
if (image != nil){
    cell.imgv.image = image
} 
else {
    cell.imgv.image = UIImage.init(named: "user")
}
})

答案 6 :(得分:0)

如果您想从 api 调用中自定义 UIImageView 的 UILabel。

import Kingfisher
func getImage(imageUrl: String,title:String?=nil){

        let someTitle = UILabel()
        view.addSubview(someTitle)
        someTitle.text = title
        someTitle.isHidden = true
       
        someTitle.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        someTitle.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        someTitle.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        someTitle.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
            let url = URL(string: imageUrl )
            yourImage.kf.setImage(
                with: url,
                placeholder:  (someTitle as? Placeholder),
                options: [
                    .loadDiskFileSynchronously,
                    .cacheOriginalImage,
                    .transition(.fade(0.25))
                ],
                completionHandler: { result in
                    // Done
                    switch result {
                    case .success(let value):
                         self.yourImage.image = value.image
                    case .failure(let error):
                        someTitle.isHidden = false
                    }
                }
            )
                        
        }
    }

只需调用控制器中的函数


getImage(imageUrl: you_url, title: your_name)