【问题标题】:A thin whiteline is been added when resize the image调整图像大小时添加细白线
【发布时间】:2011-05-21 11:18:28
【问题描述】:

当我们调整图片的大小时(下载后存储到文档目录之前),通过以下代码:

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

这会生成一个调整大小的图像,并在其方向添加细白线(就好像图像是横向的,白线添加到它的底部,如果图像是纵向的,白线添加到它的右手边)。

请告诉我,如何去掉那条白线?

谢谢。

【问题讨论】:

  • [resizedImage release];正在释放一个不属于您的对象。

标签: iphone objective-c image iphone-sdk-3.0


【解决方案1】:

虽然大小以浮点单位指定,但实际图像始终是整数个像素。

当您计算新大小以保持纵横比时,通常只有一个边作为整数像素,而其他边缩放为具有一些小数部分。然后,当您将旧图像绘制到该矩形中时,它并没有完全填充新图像。所以你看到的白线是图形系统渲染像素的方式,部分图像,部分背景。

本质上,你想做的事情不太可能,所以你需要以某种方式捏造它。有几种可能:

  • 缩放图像,使纵横比不能完全保留,但您有整数值,例如通过四舍五入:

    actualWidth = round(imgRatio * actualWidth);
    
  • 保持纵横比但剪裁小数边缘。最简单的方法可能是使图像上下文更小一点:

    UIGraphicsBeginImageContext(CGSizeMake(floor(actualWidth), floor(actualHeight)));
    [image drawInRect:rect];
    
  • 只需先用一些比白色不那么明显的颜色填充背景。显然,这是一个可怕的组合,但在适当的情况下可能会很有效,例如,如果您总是在黑色背景上绘制图像。

另外,您不能在return 之后调用任何内容,因此您最后的release 行没有执行任何操作。这也很好,因为从UIGraphicsGetImageFromCurrentImageContext 返回的图像是自动释放的——无论如何你都不应该释放它。

【讨论】:

  • 这对我来说很麻烦!我正在使用 swift,这成功了,谢谢!
  • 感谢您的回答。我们整天都在处理浮点数,我们忘记了一些关于图像的基础知识。再次感谢!
【解决方案2】:

此代码将解决您的问题:

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

【讨论】:

猜你喜欢
  • 2019-09-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多