views:

11932

answers:

4
+1  A: 

Take a look at this blog post i wrote up for some sample code to crop an image.

Lounges
+14  A: 

This post contains code for a method to resize your UIImage. The relevant portion is as follows:

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

As far as cropping goes, I believe that if you alter the method to use a different size for the scaling than for the context, your resulting image should be clipped to the bounds of the context.

Brad Larson
Makes NO SENSE as to why this fixes image orientation, but it does and thus it fixed my problem with the camera not returning the right orientation in the `originalImage`. Thanks.
Brenden
+18  A: 

I needed the same thing - in my case, to pick the dimension that fits once scaled, and then crop each end to fit the rest to the width. (I'm working in landscape, so might not have noticed any deficiencies in portrait mode.) Here's my code - it's part of a categeory on UIImage. Target size in my code is always set to the full screen size of the device.

@implementation UIImage (Extras)

#pragma mark -
#pragma mark Scale and crop image

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
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; // scale to fit height
 else
  scaleFactor = heightFactor; // scale to fit width
 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;
   }
 } 

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) 
 NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
Jane Sales
Excellent. Thank you so much!!
Carson C.
I'm trying to use the code above within a TableView to resize the thumbnails that i want to show in an uiImageView for each cell. I download the images async and before i do [UIImageView setImage:resizedImage]; i want to resize the image.. i call the method like this UIImage *resized = [originalImg imageByScalingAndCroppingForSize:CGSizeMake(64, 59)]; but when it hits [sourceImage drawInRect:thumbnailRect]; it gives me an Exec bad access error and i really don't know why.. any ideas? thank you in advance
SorinA.
curiously it works in the simulator but on the device i receive the ExecBadAccess..
SorinA.
Same here. Once moved to the device, I received intermittent errors. the code below, provided by Brad Lawson, works similarly, without the errors.
mmc
The problem with the bad exec is occurs, because the UIImage functions are not thread safe. Thats why it sometemes crashes, sometimes not
Heinrich
UIGraphicsBeginImageContext is not thread safe
Chris Beeson
+7  A: 

There's a great piece of code related to the resizing of images + several other operations. I came around this one when trying to figure ou how to resize images... http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

yonel