devC
devC

Reputation: 15

Resize UIImage for editing and save at a high resolution?

Is there a way to load a picture from the library or take a new one, resize it to a smaller size to be able to edit it and then save it at the original size? I'm struggling with this and can't make it to work. I have the resize code setup like this :

firstImage = [firstImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(960, 640) interpolationQuality:kCGInterpolationHigh];

and then i have an UIImageView :

[finalImage setFrame:CGRectMake(10, 100, 300, 232)];
 finalImage.image = firstImage;

if I setup the CGSizeMake at the original size of the picture it is a very slow process. I saw in other apps that they work on a smaller image and the editing process is fairly quick even for effects. What's the approach on this?

Upvotes: 0

Views: 2416

Answers (1)

Parth Bhatt
Parth Bhatt

Reputation: 19469

You can refer to Move and Scale Demo. This is a custom control which implements Moving and scaling and cropping image which can be really helpful to you.

Also this is the most simple code for scaling images to given size. Refer to it here: Resize/Scale of an Image

You can refer to it's code here

//  UIImage+Scale.h

@interface UIImage (scale)

-(UIImage*)scaleToSize:(CGSize)size;

@end

Implementation UIImage Scale Category With the interface in place, let’s write the code for the method that will be added to the UIImage class.

//  UIImage+Scale.h

#import "UIImage+Scale.h"

@implementation UIImage (scale)

-(UIImage*)scaleToSize:(CGSize)size
{
  // Create a bitmap graphics context
  // This will also set it as the current context
  UIGraphicsBeginImageContext(size);

  // Draw the scaled image in the current context
  [self drawInRect:CGRectMake(0, 0, size.width, size.height)];

  // Create a new image from current context
  UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

  // Pop the current context from the stack
  UIGraphicsEndImageContext();

  // Return our new scaled image
  return scaledImage;
}

@end

Using the UIImage Scale Method Calling the new scale method we added to UIImage is as simple as this:

#import "UIImage+Scale.h"

...

// Create an image
UIImage *image = [UIImage imageNamed:@"myImage.png"];

// Scale the image
UIImage *scaledImage = [image scaleToSize:CGSizeMake(25.0f, 35.0f)];

Let me know if you want more help.

Hope this helps you.

Upvotes: 1

Related Questions