0

我有一个 UIImageView,我正在使用CGAffineTransformMakeRotation. 这不仅会旋转图像,还会缩放图像。

旋转 UIImageView 并保留图像大小/比例的正确方法是什么?

我试过设置

self.image.contentMode = UIViewContentModeCenter;
self.image.autoresizingMask = UIViewAutoresizingNone;

它似乎没有帮助。

4

1 回答 1

0

试试下面的方法

-(UIImage*)rotateImage:(UIImage*)img forAngle:(CGFloat)radian   {

    UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(radian);
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    UIGraphicsBeginImageContext(rotatedSize);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, rotatedSize.width, rotatedSize.height);
    CGContextRotateCTM(context, radian);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(-img.size.width, -img.size.height, img.size.width, img.size.height), img.CGImage);
    UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return returnImg;
}
于 2013-05-30T11:00:00.697 回答