My app deals with images taken from the camera. I want to be able to resize these images to thumbnail size so I can display them inside cells on table views.
Resizing "on the fly" seems rather slow, so I was planning to resize them at the point the user imports them into the app, and store both the full size and thumbnail on my business objects, using the thumbnail for things like table views
This is the code that I use for generating thumbnails :
#import "ImageUtils.h"
@implementation ImageUtils
+(UIImage*) generateThumbnailFromImage:(UIImage*)theImage
{
UIImage * thumbnail;
CGSize destinationSize = CGSizeMake(100,100);
UIGraphicsBeginImageContext(destinationSize);
[theImage drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumbnail;
}
@end
Whilst this appears to resize the image correctly, and greatly improves the responsiveness of my tables, the scaling of the images is off.
The above will create a UIImage of 100 * 100, how can I force it to use an AspectFit or AspectFill approach?
The UIImageView I have on my table cell is 100 * 100, so I need to resize the image to fit into that, without distorting it.
Any pointers would be great!
Input:
imageSize // The image size, for example {1024,768}
maxThumbSize // The max thumbnail size, for example {100,100}
Pseudo code:
thumbAspectRatio = maxThumbSize.width / maxThumbSize.height
imageAspectRatio = imageSize.width / imageSize.height
if ( imageAspectRatio == thumbAspectRatio )
{
// The aspect ratio is equal
// Resize image to maxThumbSize
}
else if ( imageAspectRatio > thumbAspectRatio )
{
// The image is wider
// Thumbnail width: maxThumbSize.width
// Thumbnail height: maxThumbSize.height / imageAspectRatio
}
else if ( imageAspectRatio < thumbAspectRatio )
{
// The image is taller
// Thumbnail width: maxThumbSize.width * imageAspectRatio
// Thumbnail height: maxThumbSize.height
}