Friday, February 12, 2010

resize or scale UIImage


we can scale the UIImage to any size using the follwing method

add this code to your .h file

@interface UIImage (INResizeImageAllocator) + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; - (UIImage*)scaleImageToSize:(CGSize)newSize; @end

add the following to .m file

@implementation UIImage (INResizeImageAllocator) + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; { UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } - (UIImage*)scaleImageToSize:(CGSize)newSize { return [UIImage imageWithImage:self scaledToSize:newSize]; } 

e.g to resize an image
self.resizeimage = [UIImage imageWithImage:actual_image scaledToSize:CGSizeMake(65.0f, 65.0f)];

1 comment:

  1. Thanks. I was looking how to do this for the past hour. I wasn't adding the .h stuff and I didn't make an instance method.