Suppose our image object is selImage to be resized in iPhone app. See following code for this:
CGFloat newWidth = selImage.size.width;
CGFloat newHeight = selImage.size.height;
CGFloat maxWidth = [[dict valueForKey:@"MaxWidth"] floatValue];
CGFloat maxHeight = [[dict valueForKey:@"MaxHeight"] floatValue];
//Scale the Image
CGFloat scale = 0.0;
if (newWidth > maxWidth || newHeight > maxHeight)
{
if (maxWidth/newWidth < maxHeight/newHeight)
{
scale = maxWidth/newWidth;
}
else
{
scale = maxHeight/newHeight;
}
newWidth = newWidth*scale;
newHeight = newHeight*scale;
}
selImage = [self resizedImage:selImage Rect:CGRectMake(0.0, 0.0, newWidth, newHeight)];
- (UIImage *) resizedImage:(UIImage *)inImage Rect:(CGRect)thumbRect
{
UIGraphicsBeginImageContext(thumbRect.size);
[inImage drawInRect:thumbRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Hope, It helps.
No comments:
Post a Comment