A thin whiteline is been added when resize the image

前端 未结 2 754
盖世英雄少女心
盖世英雄少女心 2021-02-19 02:50

When we resizing the image (after downloading and before storing that in document directory), by the following code:

-(UIImage *)resizeImage:(UIImage *)image wit         


        
2条回答
  •  感动是毒
    2021-02-19 03:24

    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.

提交回复
热议问题