【问题标题】:Rotate UIImage custom degree旋转 UIImage 自定义度数
【发布时间】:2011-02-24 09:04:12
【问题描述】:

我想以自定义角度旋转 UIImage(不是 UIImageView) 我关注了this post,但它对我不起作用。

有人可以帮忙吗?谢谢。

更新: 下面的代码完成了一些工作,但旋转后我丢失了一些图像:

我应该改变什么才能让它正确? (顺便说一下,截图中的黄色是我的 UIImageView bg)

- (UIImage *) rotate: (UIImage *) image
{
    double angle = 20;
    CGSize s = {image.size.width, image.size.height};
    UIGraphicsBeginImageContext(s);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0,image.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGContextRotateCTM(ctx, 2*M_PI*angle/360);
    CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

【问题讨论】:

  • 嗨,如果我旋转 30 度左右的角度,它会从左侧修剪图像。

标签: iphone uiimage rotation


【解决方案1】:

这个方法返回你旋转角度的图像

#pragma mark -
#pragma mark Rotate Image 

- (UIImage *)scaleAndRotateImage:(UIImage *)image  { 

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    CGFloat boundHeight;

    boundHeight = bounds.size.height;  
    bounds.size.height = bounds.size.width; 
    bounds.size.width = boundHeight;
    transform = CGAffineTransformMakeScale(-1.0, 1.0);
    transform = CGAffineTransformRotate(transform, M_PI / 2.0); //use angle/360 *MPI

    UIGraphicsBeginImageContext(bounds.size);   
    CGContextRef context = UIGraphicsGetCurrentContext();   
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;   

}

【讨论】:

  • 这段代码旋转90度对吗?如果我只需要向左或向右旋转 5 度怎么办?
  • 太棒了! imageCopy 是否处于自动释放模式?
【解决方案2】:
- (UIImage *)rotate:(UIImage *)image radians:(float)rads
{
    float newSide = MAX([image size].width, [image size].height);
    CGSize size =  CGSizeMake(newSide, newSide);
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, newSide/2, newSide/2);
    CGContextRotateCTM(ctx, rads);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
    //CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2);

    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

这个函数可以在它的中心旋转任何图像,但是图像变成了一个正方形,所以我建议在这个函数之后绘制它时参考图像中心。

【讨论】:

  • 图像旋转但倒置。
  • 图像旋转但也翻转了
【解决方案3】:

您需要解决两个问题才能完成这项工作。

  1. 您正在围绕图像的底角而不是中心旋转
  2. 生成图像的边界矩形需要更大,现在图像已旋转以适应它。

要解决围绕中心的旋转,首先执行平移到中心,然后旋转,然后平移回来。

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);

CGContextConcatCTM(context, transform);

// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);

// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

要计算边界矩形的大小,您需要使新的旋转图像适合使用:

- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
    // Calculate the width and height of the bounding rectangle using basic trig
    CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
    CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));

    // Calculate the position of the origin
    CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2);
    CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2);

    // Return the rectangle
    return CGRectMake(newX, newY, newWidth, newHeight);
}

您可以在我以前的帖子和答案中找到这些技巧:

Creating a UIImage from a rotated UIImageView

这里:

Saving 2 UIImages

希望这会有所帮助,

戴夫

【讨论】:

  • 您好,此代码不适用于与 UIImageOrientationUp 方向不同的照片,您有什么解决方案吗?
【解决方案4】:

对于旋转图像..您可以使用此 IBAction ...每次单击按钮,图像将旋转 90 度...

-(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 和旋转度数

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

//--------------------------------------------- --------------------------

#pragma mark - imageRotatedByDegrees Method

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

}

我认为这个链接会有所帮助.......!通过单击Objective C中的按钮来旋转原始图像

http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html

【讨论】:

    【解决方案5】:

    你必须做这样的事情

    YourContainer.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );
    

    我认为其余的事情你可以想出来..

    【讨论】:

    • 除非他想旋转图像本身,而不是包含它的视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2010-11-21
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多