1

Possible Duplicate:
Any quick and dirty anti-aliasing techniques for a rotated UIImageView?

I wanted to change the angle of an UIImageView in my iPhone and app i used the following code:

previewImg.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));

and I got it working and the image is below.

enter image description here

In the image, the edges of the image is not smooth. Is there any way to smoothen the edges?

Community
  • 1
  • 1
Mithun
  • 459
  • 1
  • 10
  • 32
  • 2
    Maybe this question could help: http://stackoverflow.com/questions/1315772/how-to-do-antialiasing-on-a-rotated-view – CarlJ Jun 11 '12 at 11:03
  • 2
    Dup: http://stackoverflow.com/questions/1136110/any-quick-and-dirty-anti-aliasing-techniques-for-a-rotated-uiimageview – nhahtdh Jun 11 '12 at 11:04

1 Answers1

3

This has been asked before, check this post here, Any quick and dirty anti-aliasing techniques for a rotated UIImageView?

With that said, the optimal solution would be to create a 1px transparent border around your image.

UPDATE: Here's a helper method to add a transparent border to a UIImage, referenced from here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

- (UIImage *)transparentBorderImage:(NSUInteger)borderSize {
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];

CGRect newRect = CGRectMake(0, 0, image.size.width + borderSize * 2, image.size.height + borderSize * 2);

// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(self.CGImage),
                                            0,
                                            CGImageGetColorSpace(self.CGImage),
                                            CGImageGetBitmapInfo(self.CGImage));

// Draw the image in the center of the context, leaving a gap around the edges
CGRect imageLocation = CGRectMake(borderSize, borderSize, image.size.width, image.size.height);
CGContextDrawImage(bitmap, imageLocation, self.CGImage);
CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap);

// Create a mask to make the border transparent, and combine it with the image
CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size];
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];

// Clean up
CGContextRelease(bitmap);
CGImageRelease(borderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);

return transparentBorderImage;
}
Community
  • 1
  • 1
skram
  • 5,314
  • 1
  • 22
  • 26
  • thanks for your reply, I tried and added borderline, but how can I make it transparent? Any idea? – Mithun Jun 11 '12 at 11:44
  • Updated my answer. Hope it helps – skram Jun 11 '12 at 12:29
  • Worth noting the method above requires the `UIImage+Alpha` category from the referenced blog post. Also, I had to alter the category to make `- (CGImageRef)newBorderMask:(NSUInteger)borderSize size:(CGSize)size;` a public method instead of a private method. – Kyle Fox Jul 11 '12 at 22:17