Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My rotated image doesn't have sharp edges

I am rotating my image with the following code:

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

But it doesn't have sharp edges, does someone know a solution for this?

Here's the image I get:

enter image description here

like image 276
Ton Avatar asked Aug 01 '09 18:08

Ton


2 Answers

Using the UIImageView's CALayer to rasterize the image will provide the best antialiasing.

imageView.layer.shouldRasterize = YES;
like image 109
poetmountain Avatar answered Sep 29 '22 00:09

poetmountain


The edges of the image itself look jagged because they are being placed into a pixel grid directly, and not being interpolated. Nearest Neighbor Interpolation is the simplest kind of interpolation, where if you have pixel grid A and you move your image to pixel grid B, the pixels in grid B are chosen by simply choosing the closest pixel from grid A. Other forms of interpolation choose a weighted average of the closest pixels to arrive at the pixel value in grid B.

Your image, with its jagged edges, looks like it's using nearest neighbor interpolation, which may be the default type of interpolation on an affine transform on an iphone.

When you use some other interpolation scheme other than nearest neighbor, you'll get aliasing effects, where the subsampling isn't perfect as you transfer from one pixel grid to another. That effect makes edges in the image itself seem blurrier than they otherwise would.

like image 31
mmr Avatar answered Sep 29 '22 02:09

mmr