My rotated image has no sharp edges

I rotate my image with the following code:

CGAffineTransform rotate = CGAffineTransformMakeRotation( [ratio floatValue] ); [imageView setTransform:rotate]; 

But it has no sharp edges, does anyone know a solution for this?

Here is the image I get:

enter image description here

+6
iphone image rotation uiimageview
source share
6 answers

The edges of the image itself look jagged, because they fit directly into the pixel grid and are not interpolated. The closest neighboring interpolation is the easiest type of interpolation, where if you have a pixel grid A and you move your image to a pixel grid B, the pixels in grid B are selected by simply selecting the nearest pixel from grid A. Other interpolation forms choose the weighted average of the nearest pixels to achieve a pixel value in grid B.

Your image with its jagged edges looks like this using nearest-neighbor interpolation, which may be the default interpolation type for affine conversion to iphone.

When you use any other interpolation scheme other than the nearest neighbor, you will get aliasing effects in which the subsample is not perfect when moving from one pixel grid to another. This effect makes the edges in the image itself appear coarser than otherwise.

+2
source share

Using CALayer UIImageView to rasterize the image will provide the best smoothing.

 imageView.layer.shouldRasterize = YES; 
+12
source share

just add 1px transparent border to your image

 CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); UIGraphicsBeginImageContext( imageRect.size ); [image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
+2
source share

Any time you convert an image (excluding increments of 90 °), it will cause slightly softer edges due to pixel interpolation.

+1
source share

There is a key that you can set in Info.plist that allows you to smooth edges: UIViewEdgeAntialiasing.

As described in the following answer: When I rotate the UIImageView with the transform property, pixel pixels

0
source share

The simplest solution:

Just add this key-value pair to your Info.plist: UIViewEdgeAntialiasing is set to YES.

fooobar.com/questions/235067 / ...

0
source share

All Articles