I have a collection view has a image view set using sd webimage .this image is thumbnail image. I want to pass the actual image to another view controller .i have the URL for actual image .how to get the actual image so that pass this UIImage to next view controller. Please help me out .the language used is swift
I would recommend to use the following method from SDWebImage framework in CollectionViewCell
sd_setImage(with url: URL!, placeholderImage placeholder: UIImage!)
Then pass the URL to the next view controller instead of the actual image, and reuse the same function in the next view controller.
As the documentation clearly says, "The download is asynchronous and cached.". Which means, even if you call this function in the next view controller, it wouldn't re-download the image but will fetch from the cache.
=====================================================
Alternatively, if you want the UIImage object before pushing to next view controller. use the following method when you want to move to next view controller and use the completion block to fetch the image and pass it to next view controller.
sd_setImage(with url: URL!, completed completedBlock: SDWebImage.SDWebImageCompletionBlock!)
=====================================================
You can also use SDWebImageManager to download image without the help of an UIImageView,
make sure you import SDWebImage first,
import SDWebImage
then write a private method to handle the image download,
fileprivate func downloadImage(with url:URL) {
SDWebImageManager.shared().downloadImage(with: url, options: SDWebImageOptions(rawValue: 0), progress: nil) { (image, _, _, _, _) in
if let _ = image {
// Use the image object
} else {
// Try to download again (optional)
self.downloadImage(with: url)
}
}
}
finally, call this method using the following code,
if let actualURL = URL(string: "Your Image's Actual URL") {
self.downloadImage(with: actualURL)
}