UIImageView has a content viewing mode called
UIViewContentModeScaleAspectFill, however,
I would like to have this view written to a real, static image.
So, essentially, my image needs to be resized and cropped. I've found some code here:
Resize a UIImage the right way — Trevor’s Bike Shed
But it doesn't seem to work.
This is what I'm trying to do:
Code:
// This is the original 640x480 image (aspect ratio 1.3333)
UIImage *orgImage = [UIImage imagedName:imagePath];
// New 320x480 image (aspect ratio 0.6666)
CGSize newSize = CGSizeMake( 320, 480 );
CGRect newRect = CGRectMake( 0,0, newSize.width, newSize.height );
UIImage *retImage = [orgImage resizedImageWithContentMode:UIViewContentModeScaleAspectFill
bounds:newSize
interpolationQuality:kCGInterpolationHigh];
UIImageView *tmp = [[UIImageView alloc] initWithImage:retImage];
tmp.frame = newRect;
[self.view insertSubview:tmp atIndex:0];
[tmp release];
But, this method,
resizedImageWithContentMode, doesn't work as expected. My image is not cropped,
and its aspect ratio is not preserved. Instead, it works like AspectFit, which is wrong.
How can I resize and crop my image, while preserving its aspect ratio?
Does anyone use this code?