是否可以将 a 转换CATransform3D
为一组CIVectors
以便将 a 转换为UIImage
a CATransform3D
?
我相信现在终于可以通过遍历视图层次结构并渲染单个图像,然后将 3D 转换为图像,然后将其应用于创建的图像,来取代UIGetScreenImage()
's 将 3D 变换展平为图像的能力。UIView/CALayer
CATransform3D
我开始使用GPUImage
,它确实接受CATransform3Ds
申请 a UIImage
,但不幸的是没有运气。附件是我使用的代码,以及生成的图像的链接。
- (UIImage*)iterateLayerHierarchy:(CALayer*)layer
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(layer.frame.size.width,
layer.frame.size.height),
NO,
0.0);
for (CALayer *sublayer in layer.sublayers)
{
UIImage* subImage;
if (sublayer.sublayers.count > 1) {
subImage = [self iterateLayerHierarchy:sublayer];
} else {
subImage = [UIView getImageFromLayer:sublayer];
}
CGRect imageRect;
if (isRetina()) {
imageRect = CGRectMake(sublayer.position.x-subImage.size.width/4,
sublayer.position.y-subImage.size.height/4,
subImage.size.width,
subImage.size.height);
}else{
imageRect = CGRectMake(sublayer.position.x-subImage.size.width/2,
sublayer.position.y-subImage.size.height/2,
subImage.size.width,
subImage.size.height);
}
[subImage drawInRect:imageRect];
//sublayer.hidden = YES;
}
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
+ (UIImage*) getImageFromLayer:(CALayer *)layer
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(layer.bounds.size.width,
layer.bounds.size.height),
NO,
0.0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (!CATransform3DEqualToTransform(layer.transform, CATransform3DIdentity))
{
GPUImageTransformFilter *transformFilter = [[GPUImageTransformFilter alloc] init];
transformFilter.transform3D = layer.transform;
img = [transformFilter imageByFilteringImage:img];
}
return img;
}