Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple resizing of UIImage in XCODE

Is there any way how to resize UIImage in as few lines as possible? I don't mind of ratio, I just want to set image resolution to 80x60. That's all

like image 309
user1602687 Avatar asked Mar 15 '13 18:03

user1602687


People also ask

How do I resize an image in Xcode?

Assuming that you are referring to the layout in storyboard/IB. To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.


3 Answers

This may be overkill but, you can simply take your image, and create a graphics context at that resolution you want, then you can set the tempImage as the UIImageView, overwriting it.

        UIImage *image = YourImageView.image;
        UIImage *tempImage = nil;
        CGSize targetSize = CGSizeMake(80,60);
        UIGraphicsBeginImageContext(targetSize);

        CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
        thumbnailRect.origin = CGPointMake(0.0,0.0);
        thumbnailRect.size.width  = targetSize.width;
        thumbnailRect.size.height = targetSize.height;

        [image drawInRect:thumbnailRect];

        tempImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        YourImageView.image = tempImage;
like image 196
apollosoftware.org Avatar answered Oct 06 '22 19:10

apollosoftware.org


- (void)resizeImage:(UIImage *)image
{
   CGSize origImageSize = [image size];
   CGRect imageRect = CGRectMake(0, 0, 80, 60);
   float ratio = MAX(newRect.size.width / origImageSize.width,
                  newRect.size.height / origImageSize.height);
   UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
   UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect
                                                cornerRadius:5.0];
   [path addClip];
   CGRect imageRect;
   imageRect.size.width = ratio * origImageSize.width;
   imageRect.size.height = ratio * origImageSize.height;
   imageRect.origin.x = (newRect.size.width - imageRect.size.width) / 2.0;
   imageRect.origin.y = (newRect.size.height - imageRect.size.height) / 2.0;
   [image drawInRect:imageRect];
   UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
   NSData *data = UIImagePNGRepresentation(smallImage);
   UIGraphicsEndImageContext();
}

Do not forget to add CoreGraphics frameawork.

like image 31
rohan-patel Avatar answered Oct 06 '22 20:10

rohan-patel


Use this class (add code to .h file accordingly)

#import "UIImage+Resize.h"

@implementation UIImage (Resize)

- (UIImage *)resizedImage:(CGSize)bounds {
   return [self resizedImage:bounds upScale:YES];
}

- (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale {
   CGSize originalSize = self.size;
   float xScale = bounds.width / originalSize.width;
   float yScale = bounds.height / originalSize.height;
   float scale = MIN(xScale, yScale);

   if (!upScale) {
      scale = MIN(scale, 1);
   }

   CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);
   UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
   [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
   UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return resultImage;
}
like image 37
Obj-Swift Avatar answered Oct 06 '22 19:10

Obj-Swift