2016-02-04 18 views
13

tôi cần để có được một UIImage chỉ thay vì tải một UIImageView bình thường với Kingfisher libraryNhận UIImage chỉ với Kingfisher thư viện

Để nhận ra nó, tôi thực hiện một cách giải quyết với 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") 
    }) 

Mã này hoạt động hoàn hảo, nhưng tôi muốn làm điều đó một cách rõ ràng hơn. Có phương pháp nào trong thư viện này để chỉ nhận được UII không? Không đồng bộ và được lưu trong bộ nhớ cache

Trả lời

26

Bạn có thể sử dụng phương thức retrieveImage(with:options:progressBlock: completionHandler:)KingfisherManager cho việc này.

Có lẽ một cái gì đó như thế này:

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

Annoyingly đổi tên trong Swift 3 cho 'KingfisherManager.shared.retrieveImage (với: url! , tùy chọn: nil, progressBlock: nil) {(hình ảnh, lỗi, cacheType, imageURL) ->() trong in (hình ảnh) } ' –

+0

Điều này cũng lưu hình ảnh? – DoesData

+1

@DoesData có nó –

4

Với phiên bản mới Swift 3 bạn có thể sử dụng ImageDownloader:

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

Điều này sẽ không tận dụng bộ nhớ cache theo [dòng này] (https://github.com/onevcat/Kingfisher/blob/743e3f2a54408de212d8ec74a5c91747b6a26f7e/Sources/ImageDownloader.swift#L288) 'cachePolicy: .reloadIgnoringLocalCacheData' là điểm sử dụng Kinfisher trên bất kỳ phương pháp đơn giản nào khác –

Các vấn đề liên quan