Question

I need to rotate an UIImageView:

UIImageView *img = [[UIImageView alloc] init];
img.layer.borderWidth = 3.0;
img.layer.borderColor = UIColorFromRGB(0xffffff).CGColor;
img.layer.shadowColor = UIColorFromRGB(0x000000).CGColor;
CATransform3D transform = CATransform3DMakeRotation ([Helper degreesToRadians:(5)], 1, 1, 1);
img.layer.shadowRadius  = 2.0;
img.layer.shouldRasterize = YES;

The problem is that the border is not smooth at all:

Screen shot from the simluator

Was it helpful?

Solution

Try adding a transparent border (1px may be) around your image. See this link. Check UIImage+Alpha section in that page.

OTHER TIPS

I see most of the answer for this problem is to add 1px transparent border but it only works if we are not using any border around UIImageView, if UIImageView itself has the border and we rotate it then the border is not smooth. I am stil trying to figure out on how to fix that issue.

HERE'S WHAT I DID TO FIX THIS ISSUE

 UIImage *thumbnail = [photo thumbnailImage:thumbnailSize.width 
                           transparentBorder:2 
                                cornerRadius:0 
                        interpolationQuality:kCGInterpolationHigh];

  CGPoint randomPoint = [self getRandomOriginPoint];

  UIImageView *thumbnailView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, thumbnail.size.width, thumbnail.size.height)];

  [thumbnailView.layer setBorderWidth:2];
  [self setContentMode:UIViewContentModeCenter];
  [thumbnailView.layer setBorderColor:[UIColor whiteColor].CGColor];
  [thumbnailView.layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
  [thumbnailView.layer setShadowRadius:3.0];
  [thumbnailView.layer setShadowOpacity:1.0];
  thumbnailView.layer.shouldRasterize = YES;

I used UIImage+Resize.h from here to get the thumbnail image in my code, so with this even after you rotate image with any angle the border will look perfectly fine!

Without having to use categories, solution that made the fewest presumptions

#import <QuartzCore/QuartzCore.h>

We will use the following variable

UIView *myView;

This has been confirmed to work on UIImageViews / UIViews of all types

myView.layer.shouldRasterize = YES; //the single line that solved this problem from the above

You should not need to add a border of any type for this to work.

 CATransform3D rotate = CATransform3DIdentity;
 myView.layer.shouldRasterize = YES;
 rotate = CATransform3DRotate(rotate,M_PI_4/8,0.0,0.0,1);
 myView.layer.transform = rotate;

Note: This answer is provided to consolidate the information into a single answer that removes some of the other requirements. In this example I use M_PI_4/8 as it's a nice viewing angle for if you have a Stickie Note sort of view that you are implementing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top