CGImage memory size returning weird results

327 views Asked by At

I'll make it quick and straightforward.

I'm using this method to calculate 'UIImage' memory size.

extension UIImage {

   func memorySize() -> size_t {
        let image = self.cgImage!
        let size =  image.height * image.bytesPerRow
        return size
    }
}

I'm cropping an UIImage. Before cropping our image ( Meaning using the original image). This is the image stats : Width -> 2668 | Height -> 2000 | scale -> 1 | memorySize -> 21,376,000.

After cropping the original image using

 func passCroppedImage(_ myFrame : CGRect) -> UIImage {
        let imageRef:CGImage = preview.image!.cgImage!.cropping(to: rect)!
        let croppedImage:UIImage = UIImage(cgImage: imageRef)
        return croppedImage
}

My cropped image stats are : Width -> 1126 | Height -> 2000 | scale -> 1 | memorySize -> 21,376,000.

How is it possible, that the cropped image width/height size is different, but the memory size remains the same?

I've made a small research, according to apple docs, about image.cgImage!.cropping(to: rect) -> CGImage

   The resulting image retains a reference to the original image,
   which means you may release the original image after calling this function.

But again, if i'm trying to release the CGImage, it says Swift handing the memory management. I'm confused. ANY help would be much appreciated!

0

There are 0 answers