swift2 仅使用Kingfisher库获取UIImage

xoefb8l8  于 2022-11-06  发布在  Swift
关注(0)|答案(8)|浏览(271)

我只需要获取UIImage,而不是使用Kingfisher library加载普通UIImageView
为了实现这一点,我使用UIImageView实现了一个变通方案:

let imageView = UIImageView()

imageView.kf_setImageWithURL(NSURL(string: cpa.imageName)!, placeholderImage: nil,
        optionsInfo: [.Transition(ImageTransition.Fade(1))],
        progressBlock: { receivedSize, totalSize in
            print("\(receivedSize)/\(totalSize)")
        },
        completionHandler: { image, error, cacheType, imageURL in
            anView!.image = image //anView IS NOT an UIImageView
            anView!.frame.size = CGSize(width: 15.0, height: 15.0)
            print("Finished")
    })

这段代码运行得很好,但我想用一种更干净的方式来完成它。这个库中是否有一个方法可以只获取UIImage?异步和缓存

sg24os4d

sg24os4d1#

您可以使用KingfisherManagerretrieveImage(with:options:progressBlock: completionHandler:)方法来执行此操作。
也许是这样的:

KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
    print(image)
})
ncecgwcz

ncecgwcz2#

这是在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
svmlkihl

svmlkihl3#

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

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

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

56lgkhnf

56lgkhnf4#

雨燕五:
我将在**@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.
 }
h9a6wy2h

h9a6wy2h5#

Kingfisher 5中的另一种方式:

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

myzjeezk6#

翠鸟5号

imageView.kf.setImage(with: url) { result in
   switch result {
   case .success(let value):
       print("Image: \(value.image). Got from: \(value.cacheType)")
   case .failure(let error):
       print("Error: \(error)")
   }
 }

查看更多信息:https://github.com/onevcat/Kingfisher/wiki/Kingfisher-5.0-Migration-Guide

jyztefdp

jyztefdp7#

翠鸟5号
此代码100%有效

YourImageView.kf.setImage(with: URL(string: imagePath), placeholder: nil, options: nil, progressBlock: nil, completionHandler: { result in
switch result {
    case .success(let value):
                print("Image: \(value.image). Got from: \(value.cacheType)")
    case .failure(let error):
                print("Error: \(error)")
    }
})

//OR
let resource = ImageResource(downloadURL: picUrl!)
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)")
        imageProfile = value.image
        case .failure(let error):
            print("Error: \(error)")
        }
    }
2admgd59

2admgd598#

如果要自定义UILabel,请从API调用UIImageView;
第一个

只需调用控制器中的函数

getImage(imageUrl: you_url, title: your_name)

相关问题