UIImageView:变换UIImage [英] UIImageView: get transformed UIImage

查看:189
本文介绍了UIImageView:变换UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它旋转UIImageView:

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

现在我想获得旋转图像。我可以这样做吗?我试图使用CGImageRotatedByAngle函数使用CoreGraphics来接受这个问题的接受答案,但它会改变坐标slighlty不正确(最后我们得到标记为错误的第三个屏幕截图)。正如您所看到的,图像会丢失其原始大小并被裁剪。我的目标是获得原始大小的中央镜头和旋转指定角度的图像。我的意思是要求旋转形状的大小保持不变,但新图像当然会比初始大。

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.

推荐答案

好的,我正在回答再次提出我自己的问题,希望它对未来的某些人有用...
回到CGImageRotatedByAngle,正如我之前所说,它提供了几乎所需的效果,但坐标系翻译顺序必须略有改变。这是我获得所需结果的方式:

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

这篇关于UIImageView:变换UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆