这种方法有什么问题?问题是旋转后的图像有透明框。我怎么能摆脱它?
@implementation UIImage (RotationMethods)
static CGFloat getRadianFromDegree(CGFloat degrees)
{return degrees * M_PI / 180;};
- (UIImage *) rotateImageByDegree:(CGFloat)degrees
{
UIView *rotatedImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(getRadianFromDegree(degrees));
rotatedImageView.transform = t;
CGSize rotatedSize = rotatedImageView.frame.size;
UIGraphicsBeginImageContextWithOptions(rotatedSize, YES, self.scale);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, getRadianFromDegree(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rotatedImage;
}
@end
EDIT2 以下是旋转动作的代码:
- (IBAction)rotateAction:(id)sender {
self.imageView.image = [self.imageView.image rotateImageByDegree:22];
}