Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView: get transformed UIImage

I'm rotating UIImageView using this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myImgView.transform = CGAffineTransformMakeRotation(angle*M_PI/180);
[UIView commitAnimations];

Now I want to get rotated image. Can I do this? I was trying to make it with CoreGraphics using CGImageRotatedByAngle function from accepted answer for this question but it transforms coordinates slighlty incorrect (finally we get third screenshot marked as wrong). As you can see the image loses its original size and is cropped. My goal is to get image like on central shot with original size and rotated for specified angle. I mean it's required that the size of rotated shape stay unchanged but new image of course is gonna be larger than initial.

enter image description here

like image 527
heximal Avatar asked Dec 26 '22 19:12

heximal


1 Answers

OK, I'm answering my own question again, hopefully it will be useful for somebody in future... Coming back to CGImageRotatedByAngle as I previously said it gives almost required effect but coordinate system translation sequence must be slighly altered. And here is the way I got the desired result:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle {

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM( bmContext, 0.5f * rotatedRect.size.width, 0.5f * rotatedRect.size.height ) ;
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextDrawImage(bmContext, CGRectMake(-imgRect.size.width* 0.5f, -imgRect.size.height * 0.5f,
                                             imgRect.size.width,
                                             imgRect.size.height),
                       imgRef);
    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}
like image 51
heximal Avatar answered Jan 16 '23 10:01

heximal