A thin whiteline is been added when resize the image
When we resizing the image (after downloading and before sto开发者_如何学Goring that in document directory), by the following code:
-(UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = newSize.width/newSize.height;
if(imgRatio!=maxRatio){
if(imgRatio < maxRatio){
imgRatio = newSize.width / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = newSize.width;
}
else{
imgRatio = newSize.height / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = newSize.height;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[resizedImage release];
return [resizedImage autorelease];
}
this produce a re sized image with the thin white line added towards it's orientation(as if image is landscape white line is added to it's bottom and if image is portrait white line is added to it's right hand).
please tell that, how to get rid of that white line?
Thank you.
Although the size is specified in floating point units, the actual image is always an integral number of pixels.
When you calculate the new size to preserve the aspect ratio, you will typically have only one of the sides as a whole number of pixels, while the other scales to have some fractional part. When you then draw the old image into that rect, it doesn't quite fill the new image. So what you see as a white line is the graphics system's way of rendering the pixels that are part image, part background.
In essence, what you want to do is not quite possible, so you need to fudge it somehow. There are several possibilities:
Scale the image such that the aspect ratio is not perfectly preserved but you have integral values, for example by rounding:
actualWidth = round(imgRatio * actualWidth);
Maintain the aspect ratio but clip the fractional edge. The easiest way to do this is probably to make the image context a little smaller:
UIGraphicsBeginImageContext(CGSizeMake(floor(actualWidth), floor(actualHeight))); [image drawInRect:rect];
Just fill the background first with some colour that's less obvious than white. This is a dreadful kludge, obviously, but could be effective in the right circumstances, for example if you're always drawing the image against a black background.
On a separate note, you can't call anything after return
, so your final release
line isn't doing anything. This is just as well because the image returned from UIGraphicsGetImageFromCurrentImageContext
is autoreleased -- you should not be releasing it anyway.
This code will fix your problem:
+ (UIImage *)scaleImageProportionally:(UIImage *)image {
if (MAX(image.size.height, image.size.width) <= DEFAULT_PHOTO_MAX_SIZE) {
return image;
}
else {
CGFloat targetWidth = 0;
CGFloat targetHeight = 0;
if (image.size.height > image.size.width) {
CGFloat ratio = image.size.height / image.size.width;
targetHeight = DEFAULT_PHOTO_MAX_SIZE;
targetWidth = roundf(DEFAULT_PHOTO_MAX_SIZE/ ratio);
}
else {
CGFloat ratio = image.size.width / image.size.height;
targetWidth = DEFAULT_PHOTO_MAX_SIZE;
targetHeight = roundf(DEFAULT_PHOTO_MAX_SIZE/ ratio);
}
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
targetWidth = targetSize.width;
targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if (!CGSizeEqualToSize(imageSize, targetSize)) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = roundf(width * scaleFactor);
scaledHeight = roundf(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);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (newImage == nil) NSLog(@"could not scale image");
return newImage;
}
}
精彩评论