Roi Mulia Roi Mulia - 2 years ago 111
Swift Question

Understanding UIImage size property compare to UIView frame size Property

I'm trying to scale down UIImage to fit the exact same size my UIView has, Tho i'm not sure what is the exact relations between them.
This is the way i'm checking if my UIImage bigger than my UIView.

let rect = preview.frame
if croppedImage.size.width * croppedImage.scale > rect.size.width * UIScreen.

Is this the correct way? Does the values speaking at the same dimensions?

Answer Source

From the UIImage class reference, referring to the scale property:

If you multiply the logical size of the image (stored in the size property) by the value in this property, you get the dimensions of the image in pixels.

From the UIWindow class reference, referring to its own scale property:

This value reflects the scale factor needed to convert from the default logical coordinate space into the device coordinate space of this screen. The default logical coordinate space is measured using points. For standard-resolution displays, the scale factor is 1.0 and one point equals one pixel. For Retina displays, the scale factor is 2.0 and one point is represented by four pixels.

So you should be able to compare croppedImage.size.width * croppedImage.scale with rect.size.width * UIScreen.scale. However, you may want to consider placing your UIImage into a UIImageView. Then you can set the frame property on the UIImageView to your view's frame and voila! (Note, however, that this may stretch your image.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download