Image size

From time to time I need to know the width and height of the images. I am using the following code:

UIImage *imageU = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myImage.png"]];

CGFloat imageW = CGImageGetWidth(imageU.CGImage);
CGFloat imageH = CGImageGetHeight(imageU.CGImage);

My question is if there is any other way to know the width and height of the image, in pixels, without having to load the image onto a variable, possibly consuming memory. Can dimensions be read directly from the file without downloading the entire image?

This should work for PNG and JPEG.

thank.

+3
source share
2 answers

You can parse a PNG file containing size information in the header. See Get image size without loading into memory .

(, imageU.size CGSize .)

+1

, "", . NSInputStream / IHDR ( @KennyTM), .

0

Source: https://habr.com/ru/post/1743660/


All Articles