UIImage iPhone 旋转 37.8 度

发布于 2024-08-17 15:39:30 字数 90 浏览 2 评论 0原文

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

Objective-C中有没有可以实现图像旋转的函数?

伊恩

After I load an image from the device, I need to rotate it 37.8 degrees then display it on a View.

Is there an function in Objective-C that can do the image rotation?

Ian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-08-24 15:39:30

旋转视图:

imageView.transform = CGAffineTransformMakeRotation(37.8°);

旋转图像,

  1. 计算旋转后图像所占用的宽度和高度。
  2. 通过UIGraphicsBeginImageContext创建一个CGContext
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext(); 并使用此图像。
  6. 释放上下文。

To rotate the view:

imageView.transform = CGAffineTransformMakeRotation(37.8°);

To rotate the image,

  1. Calculate the width and height will be occupied by the image after rotation.
  2. Create a CGContext by UIGraphicsBeginImageContext.
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext(); and use this image instead.
  6. Release the context.
盛夏已如深秋| 2024-08-24 15:39:30

是的,请参阅我对此问题的回答:有关旋转滑块的问题

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

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

Yes, see my answer to this question: Question about rotating a slider

To convert degrees to radians (for the positionInRadians arg) use this function:

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
回心转意 2024-08-24 15:39:30

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

-(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;

}

请参阅 此链接了解更多信息。

To rotate the image, try this:

-(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);

}

This method allows you to rotate an image an arbitrary amount:

- (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;

}

See this link for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文