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
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.
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.
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;
                        - (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.
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;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With