简体   繁体   中英

setImageWith Url completion block swift ios

I am new to swift. I am loading image with url

mainImageView.setImageWith(URL(string: ("https:" + (content?.imagePath)!)), placeholderImage: nil)
print("dimensions after loading \(String(describing: mainImageView.image?.size))")

In case, I print the dimensions of the image as shown above then dimensions come out to be 21*6.5. However, if I wait for sometime and then print the dimensions are 188*109. How do I add a completion block to setImageWith so that I can come to know when image has finished loading?

You can use Sdwebimage for loading the image with completion block https://github.com/rs/SDWebImage

imageView.sd_setImageWithURL(NSURL(string: urlString), completed: {
                (image, error, cacheType, url) in
                // you can get the image width here...
            })

It is happening because URL will always take a time to load image thats why first you got 21*6.5 dimensions and then got real dimensions 188*109 .

As best way to prefer 3rd party library SDWebImage that will manage all the thing, You just need to set image URL.

There is method name is

open func sd_setImage(with url: URL!, placeholderImage placeholder: UIImage!, options: SDWebImageOptions = [], completed completedBlock: SDWebImage.SDWebImageCompletionBlock!)

that has completion block so you can manage whatever you want.

Convert image URL into data then Data into UIIamge , Here is a function:

func getImageFromUrl(_ strUrl: String, completionHandler handler: @escaping (_ img: UIImage) -> Void) {
    DispatchQueue.global(qos: .background).async {
        let url = URL(string: strUrl)
        let dataFromUrl = Data(contentsOf: url!)
        if dataFromUrl == nil {
            return
        }
        DispatchQueue.main.async(execute: {() -> Void in
            handler(UIImage(data: dataFromUrl!))
        })
    })
}

Use This: -

  let imageCache = NSCache<AnyObject, AnyObject>()
 typealias CompletionHandler = (_ success:Bool, _ image:UIImage?) -> Void





 func loadImageUsingCacheWithUrlString(_ urlString: 
 String,completionHandler: @escaping CompletionHandler) {
let image = UIImage()

    //check cache for image first
    if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
        image = cachedImage
        completionHandler(true, image!)
        return
    }

    if urlString.characters.count == 0 {
        completionHandler(false, image)
        return
    }

    //otherwise fire off a new download
    let url = URL(string: urlString)

    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

        //download hit an error so lets return out
        if error != nil {
            print(error ?? "")
            completionHandler(false,nil)
            return
        }

        DispatchQueue.main.async(execute: {
            if let downloadedImage = UIImage(data: data!) {
                image = downloadedImage
                imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)                    

                completionHandler(true,image)
            }
        })

    }).resume()


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM