从uiimageview获取旋转的图像

时间:2012-04-05 17:40:52

标签: objective-c ios uiimage core-graphics cgaffinetransform

我创建了一个应用程序,我可以使用手势旋转,重新调整大小,翻译图像。然后我需要从UIImageView获取图像。我在 Stack-overflow 的某些地方找到了这部分代码。尽管smiler问题已经回答here,但它需要输入角度。同一个人在其他地方写了一个更好的解决方案,我正在使用它。但它有一个问题。通常它会返回一个空白图像。或截断的图像(通常来自顶部)。所以代码有问题,需要进行一些更改。我的问题是,我是Core-graphics的新手并且严重陷入了这个问题。

UIGraphicsBeginImageContext(imgView.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = imgView.transform;
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake(0, 0, imgView.image.size.width, imgView.image.size.height), imgView.image.CGImage);
UIImage *newRotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
UIGraphicsEndImageContext();

编辑1.1 感谢您的示例代码,但同样有问题。让我更详细地解释一下,我正在使用手势来使用imageview缩放,翻译和调整图像大小。因此,所有这些数据都保存在imageview的transform属性中。我喜欢核心图像中的另一种方法。所以我将代码更改为:

CGRect bounds =  CGRectMake(0, 0, imgTop.size.width, imgTop.size.height);
CIImage *ciImage = [[CIImage alloc] initWithCGImage:imageView.image.CGImage options:nil];
CGAffineTransform transform = imgView.transform;
ciImage = [ciImage imageByApplyingTransform:transform];
return [UIImage imageWithCIImage:ciImage] ;

现在我得到了挤压和错误尺寸的镜像图像。很抱歉再次打扰你。你能指导我如何在coreimage中使用imageview的变换来获得正确的图像吗?

4 个答案:

答案 0 :(得分:3)

CIImage *ciImage = [[CIImage alloc] initWithCGImage:fximage.CGImage options:nil];
CGAffineTransform transform = fxobj.transform;
float angle = atan2(transform.b, transform.a);
transform = CGAffineTransformRotate(transform, - 2 * angle);
ciImage = [ciImage imageByApplyingTransform:transform];
UIImage *screenfxImage = [UIImage imageWithCIImage:ciImage];

请记住添加代码:transform = CGAffineTransformRotate(transform, - 2 * angle);因为旋转方向相反

答案 1 :(得分:1)

我为这种事创建了一个Objective-C类。你可以在GitHub上查看ANImageBitmapRep。以下是轮换的方法:

ANImageBitmapRep * ibr = [myImage image];
[ibr rotate:anAngle];
UIImage * rotated = [ibr image];

请注意,此处anAngle为弧度。

答案 2 :(得分:1)

以下是文档的链接: - http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html

旋转图片的示例代码: -

        CIImage *inputImage = [[CIImage alloc] initWithImage:currentImage];
        CIFilter * controlsFilter = [CIFilter filterWithName:@"CIAffineTransform"];
        [controlsFilter setValue:inputImage forKey:kCIInputImageKey];
        [controlsFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputAngle"];
        CIImage *displayImage = controlsFilter.outputImage;
        UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

        CIContext *context = [CIContext contextWithOptions:nil];
        if (displayImage == nil || finalImage == nil) {
            // We did not get output image. Let's display the original image itself.
            photoEditView.image = currentImage;
        }
         else {

            CGImageRef imageRef = [context createCGImage:displayImage fromRect:displayImage.extent];
            photoEditView.image = [UIImage imageWithCGImage:imageRef];
            CGImageRelease(imageRef);
        }
        context = nil;
        [inputImage release];

答案 3 :(得分:0)

我创建了示例应用程序以在目标C中执行此操作(减去缩放部分)。如果有人感兴趣,可以在此处下载:https://github.com/gene-code/coregraphics-drawing/tree/master/coregraphics-drawing/test