在UIImageView中旋转图像

时间:2011-08-25 13:46:30

标签: iphone objective-c ios uiimageview uiimage

是否可以在UIImageView中仅旋转图像?我正在寻找有关它的信息,但我只找到了旋转UIImageVeiw的信息。

5 个答案:

答案 0 :(得分:15)

您可以使用以下代码旋转图像。注意,这使用了CGImageRef,您可以通过

从UIImage获取
CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30];

获得旋转的图像后,您可以将ImageView的图像设置为新的旋转图像,如下所示:

UIImage* img = [UIImage imageWithCGImage: imageRef];
myImageView.image = img;

这是一个旋转imageRef:

的方法
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(bmContext,
                          +(rotatedRect.size.width/2),
                          +(rotatedRect.size.height/2));
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextTranslateCTM(bmContext,
                          -(rotatedRect.size.width/2),
                          -(rotatedRect.size.height/2));
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             rotatedRect.size.width,
                                             rotatedRect.size.height),
                       imgRef);



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}

答案 1 :(得分:6)

看看旋转ImageView的图层。您需要导入QuartzCore库才能使其可访问。

myImageView.layer.transform = CGAffineTransformMakeRotation (1.5);

答案 2 :(得分:5)

请试试这个,它对我有用:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:10];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
祝你好运!

答案 3 :(得分:2)

以下代码适用于Swift:

let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)
self.imageView.image = img
self.imageView.setNeedsLayout()

答案 4 :(得分:0)

如果您需要旋转/翻转图像以进行本地化,可以使用以下内容: imageView.image = imageView.image?.imageFlippedForRightToLeftLayoutDirection()