调整图像大小时添加了细白线 [英] A thin whiteline is been added when resize the image

查看:124
本文介绍了调整图像大小时添加了细白线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When we resizing the image (after downloading and before storing 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?

谢谢。

推荐答案

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

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


  • 首先填充背景,其中一些颜色不如白色。显然,这是一个可怕的kludge,但在适当的情况下可能会有效,例如,如果你总是在黑色背景下绘制图像。

  • 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.

    另外,在返回之后你不能再打电话了,所以最后的版本行没有做任何事情。这也是因为从 UIGraphicsGetImageFromCurrentImageContext 返回的图像是自动释放的 - 你不应该释放它。

    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.

    这篇关于调整图像大小时添加了细白线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆