Pages

June 16, 2013

Content mode for scaled, resized or cropped image

How to scale or resize or crop a UIImage image in order to fit in a box? This is my implementation.


/** Returns the appropriate content mode for correctly viewing an image inside a container.

 Default behaviour is: if the image is larger (in width) than container then it'll be resized; if it's longer (in height) it'll be cropped at the bottom.

 @param imageSize The size of the image.
 @param limits The bounds of the container.
 @return Returns the right UIViewContentMode value.

 */
+ (UIViewContentMode)contentModeForImageOfSize:(CGSize)imageSize insideBounds:(CGSize)limits {

    if (imageSize.width > limits.width) {
        return UIViewContentModeScaleAspectFit;
    } else {
        return UIViewContentModeCenter;
    }
}



Download the ready-for-use source file (.m) of Content mode for scaled, resized or cropped image

No comments:

Post a Comment