旋转UIImage自定义度数

4
我想要以自定义度数旋转UIImage(而不是UIImageView)。我遵循了this post,但对我没用。
有人可以帮忙吗?谢谢。
更新: 下面的代码完成了部分工作,但在旋转后我失去了一些图像:

enter image description here

我应该做什么更改才能做到正确?(顺便说一下,截图中的黄色是我的UIImageView背景)
- (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度左右,它会从左侧修剪图像。 - Urmil Setia
5个回答

8

该方法返回您旋转角度后的图像

#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度,该怎么办? - deaa
1
太好了!imageCopy是否处于自动释放模式? - Martin

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

这个函数可以将任何图像以其中心旋转,但是图像会变成正方形,所以我建议在此函数后绘制图像时参考图像中心。


图像已经旋转,但是是倒置的。 - Guy
图像旋转了,但也翻转了。 - Dev2rights

2

为使这个程序工作,您需要注意以下两点:

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

您可以在我的先前的帖子和答案中找到这些技术,链接如下: 从旋转的UIImageView创建UIImage 以及: 保存两个UIImages 希望这对您有所帮助,
Dave

嗨,这段代码无法处理方向与UIImageOrientationUp不同的照片,你有什么解决方案吗? - Pion

0

对于旋转图像...您可以使用此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


-3

你需要做类似这样的事情

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

我认为你可以自己解决剩下的事情。


5
除了他想要旋转图像本身,而不是包含它的视图。 - Nyx0uf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接