How to resize the UIImage in objective-c?

0 votes
65 views
asked Feb 5, 2015 by anonymous
Hi all

  How to resize the UIImage in objective-c?

1 Answer

0 votes
answered Feb 5, 2015 by admin (8,115 points)

 

+ (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize UIImage:(UIImage*)image{

    

    UIImage *sourceImage = image;

    UIImage *newImage = nil;

    

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    

    CGFloat targetWidth = targetSize.width;

    CGFloat targetHeight = targetSize.height;

    

    CGFloat scaleFactor = 0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

        

        if (widthFactor < heightFactor)

            scaleFactor = widthFactor;

        else

            scaleFactor = heightFactor;

        

        scaledWidth  = width * scaleFactor;

        scaledHeight = height * scaleFactor;

        

        // center the image

        

        if (widthFactor < heightFactor) {

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        } else if (widthFactor > heightFactor) {

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

<p style="margin: 0px; font-size: 11px; font-family: Menl
...