【问题标题】:Apply CATransform3D on UIImage while drawing on context在绘制上下文时在 UIImage 上应用 CATransform3D
【发布时间】:2017-01-03 06:42:12
【问题描述】:

我有几个UIImageView 并在其图层属性上应用了 CATransform3D,现在我想获得一个图像(将所有图像合并为一个并进行转换)。

为此,我使用 CoreGraphics 框架在上下文中绘制图像,然后获取结果图像。

需要实现这个:

  • 使用CATransform3D 在上下文中绘制每个图像,然后从图像上下文中获取图像。

我试过了,但没有成功。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, CATransform3DGetAffineTransform(imageView.layer.transform));
CGContextDrawImage(context, (CGRect){ CGPointZero, imageView.image.size }, [imageView.image CGImage]);
CGContextTranslateCTM(context, 0, imageView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

我也尝试过 CoreImage 框架,但仍然没有运气。 :(

CIImage *ciImage = [CIImage imageWithData:UIImagePNGRepresentation(imageView.image)];
CIContext *context = [CIContext contextWithOptions:nil];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];

CIRectangleFeature *rect = (CIRectangleFeature *)[detector featuresInImage:ciImage options:@{CIDetectorImageOrientation : [NSNumber numberWithInt:1]}].firstObject;

CIFilter *filterMain = [CIFilter filterWithName:@"CIPerspectiveCorrection"];
[filterMain setValue:[CIVector vectorWithCGPoint:rect.topLeft] forKey:@"inputTopLeft"];
[filterMain setValue:[CIVector vectorWithCGPoint:rect.topRight] forKey:@"inputTopRight"];
[filterMain setValue:[CIVector vectorWithCGPoint:rect.bottomRight] forKey:@"inputBottomRight"];
[filterMain setValue:[CIVector vectorWithCGPoint:rect.bottomLeft] forKey:@"inputBottomLeft"];
[filterMain setValue:sourceCIImage forKey:kCIInputImageKey];

CIImage *outputImage = [filterMain valueForKey:kCIOutputImageKey];
UIImage *resultImage = [UIImage imageWithCIImage:outputImage];

about 行代码给了我一些奇怪的结果。

我尝试过的另一件事是drawViewHierarchyInRect:afterScreenUpdates: 方法,但它给我的图像非常模糊。

那么如何使用 CATransform3D 在上下文中绘制图像?如果有人遇到过此类问题并已解决,请分享您的想法。任何帮助将不胜感激。

注意:我不是在谈论 CGAffineTransformation 它的 CATransform3D

【问题讨论】:

    标签: ios objective-c uiimageview uiimage catransform3d


    【解决方案1】:

    Apple 提供了一个名为 CoreImage 的框架,您可以通过应用过滤器来获得转换后的图像。您可以使用CIPerspectiveTransform 过滤器通过图像/图像视图的四个角(TopLeft、TopRight、BottomLeft 和 BottomRight)来获取转换后的图像。

    我已经回答了这种问题here...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多