旋转UIImage自定义度

时间:2011-02-24 09:04:12

标签: iphone uiimage rotation

我想在自定义度上旋转UIImage(而不是UIImageView) 我跟着this post,但它对我不起作用。

任何人都可以提供帮助?感谢。

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

enter image description here

为了使它正确,我应该改变什么? (顺便说一下截图中的黄色是我的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;

}

5 个答案:

答案 0 :(得分: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;   

}

答案 1 :(得分: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;
}

此功能可旋转其中心的任何图像,但图像变为方形,因此我建议在此功能后绘制时引用图像中心。

答案 2 :(得分:2)

你需要解决两件事才能使其发挥作用。

  1. 您正在围绕图像的底角而不是中心
  2. 进行旋转
  3. 当图像旋转以适应时,所得图像的边界矩形需要更大。
  4. 要解决围绕中心的旋转,首先执行平移到中心,然后旋转,然后平移回来。

    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

    希望这有帮助,

    戴夫

答案 3 :(得分: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

答案 4 :(得分:-2)

你必须做这样的事情

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

我认为你能想到的其余部分......