JJgendarme
JJgendarme

Reputation: 1401

Iphone SDK: compare image sizes

How can I compare image sizes?

I tried doing something like this: if (image1.image.size > image2.image.size) {}

I failed :(

Can somebody tell me how size-comparsion works?

Upvotes: 0

Views: 786

Answers (2)

salo.dm
salo.dm

Reputation: 2315

The size property of UIImage is a C-struct consisting of two parameters, width & height. To compare sizes, you might compare the total area of each image. If you are comparing UIImages, the following code will do:

if (image1.size.width * image1.size.height > image2.size.width * image2.size.height) {}

Note that your code, if it's referring to UIImages, has an extra image.

If you are, however, comparing UIImageViews, it would probably be preferable to compare the frames. I'm not sure whether the image size may diverge from the frame in some cases, like when the image is scaled according to the UIView property contentMode. (Note that UIImageView inherits from UIView.) So, to compare frames, the code would be as follows:

if (imageView1.frame.size.width * imageView1.frame.size.height > imageView2.frame.size.width * imageView2.frame.size.height) {}

Upvotes: 1

knuku
knuku

Reputation: 6102

Maybe it's worth to compare their areas?

if (image1.image.size.width * image1.image.size.height > image2.image.size.width *image2.image.size.height) 
{
    //Do smth
}

You should decide yourself what "larger" does mean.

Upvotes: 3

Related Questions