UIImage iPhone旋转37.8度

时间:2010-01-11 18:13:48

标签: iphone uiimage rotation

从设备加载图像后,我需要将其旋转37.8度,然后将其显示在视图上。

Objective-C中是否有可以进行图像旋转的功能?

伊恩

3 个答案:

答案 0 :(得分:6)

旋转视图:

imageView.transform = CGAffineTransformMakeRotation(37.8°);

要旋转图像,

  1. 计算旋转后图像占据的宽度和高度。
  2. CGContext创建UIGraphicsBeginImageContext
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext();并改为使用此图片。
  6. 发布背景信息。

答案 1 :(得分:1)

是的,请参阅我对这个问题的回答:Question about rotating a slider

要将度数转换为弧度(对于positionInRadians arg),请使用此函数:

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};

答案 2 :(得分:1)

要旋转图像,请尝试以下操作:

-(IBAction)rotateImageClick:(id)sender{

    UIImage *image2=[[UIImage alloc]init];
    image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
    self.roateImageView.image = image2;
    imgData= UIImageJPEGRepresentation(image2,0.9f);

}

此方法允许您以任意数量旋转图像:

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{

    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];

    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
    rotatedViewBox.transform = t;

    CGSize rotatedSize = rotatedViewBox.frame.size;
    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, (degrees * M_PI / 180));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

有关详细信息,请参阅this link