Search code examples
iosswiftsdwebimage

How to know Image file Size when using SDWebImages?


I'm using SDWebImages and set image using imageView.sd_setImage(), now I want to know what is size of image I downloaded.

I want size in height, width and total memory it occupy.


Solution

  • Try this:

    imageView.sd_setImage(with: URL(string: imgUrl), completed: { (image, error, cache, url) in
                    let imageSize = image?.size
                    print("imageSize", imageSize)
                    var imgData: NSData = NSData(data: image.jpegData(compressionQuality: 1))
                    // var imgData: NSData = UIImagePNGRepresentation(image)
                    // you can also replace UIImageJPEGRepresentation with UIImagePNGRepresentation.
                    var imageSizeBytes: Int = imgData.count
                    print("size of image in KB: %f ", Double(imageSizeBytes) / 1000.0)
                })