2011-05-21 40 views
14

当我们调整大小的图像(下载之后并存储在文件目录之前),通过下面的代码:薄WHITELINE是被添加时调整图像大小

-(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]; 
} 

此产生与重新尺寸图像细的白线添加到它的方向(如果图像是横向白线添加到它的底部,如果图像是纵向白线添加到它的右手)。

请说清楚,如何摆脱那条白线?

谢谢。

+1

[resizedImage发布];正在释放一个你不拥有的物体。 – MattyG 2011-09-13 05:29:40

回答

19

尽管大小是以浮点单位指定的,但实际图像始终是像素的整数。

当您计算新的尺寸以保留宽高比时,通常只有一个边作为像素的整体数量,而其他尺寸只有一些小数部分。然后,当您将旧图像绘制到该矩形中时,它不会完全填充新图像。所以你看到的白线就是图形系统渲染零件图像和部分背景像素的方式。

从本质上讲,你想做的事情不太可能,所以你需要以某种方式对它进行调整。有几种可能性:

  • 缩放图像,使得纵横比不保存完好,但你通过舍入具有整数值,例如:

    actualWidth = round(imgRatio * actualWidth); 
    
  • 保持纵横比,但是剪辑分数边缘。要做到这一点最简单的方法可能是让图像内容更小一点:

    UIGraphicsBeginImageContext(CGSizeMake(floor(actualWidth), floor(actualHeight))); 
    [image drawInRect:rect]; 
    
  • 只是第一填充背景与一些颜色比白色不太明显。这显然是一个可怕的混乱,但在正确的情况下可能会有效,例如,如果您总是在黑色背景下绘制图像。

在一个单独的说明,你不能return后叫什么,所以你的最终release线没有做任何事情。这同样如此,因为从UIGraphicsGetImageFromCurrentImageContext返回的图像是自动发布的 - 您不应该将其释放。

+0

这对我来说非常麻烦!我使用的是快捷方式,而且这样做的窍门,谢谢! – blackops 2016-04-13 23:32:25

+0

感谢您的回答。我们花了整整一天的时间处理浮点数,我们忘记了一些关于图像的基础知识。再次感谢! – 2017-01-07 21:07:26

0

此代码将解决您的问题:

+ (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; 
} 
} 
+0

http://stackoverflow.com/a/6081650/656600已经解决了我的问题,超过2年前。但是,谢谢乔治! – rptwsthi 2013-12-20 13:58:44

+0

@rptwsthi,但它不能帮助我:( – George 2013-12-26 12:23:42