iPhone - is it IMPOSSIBLE to grab the contents of a CALayers composition?

社会主义新天地 提交于 2019-11-30 16:10:02

问题


I have a CALayer transformed in 3D on a offscreen UIView (much larger than 320x480).

How do I dump what is seen on this UIView to a UIImage?

NOTE: I Have edited the question to include this code...

This is how I create the layer...

CGRect area = CGRectMake (0,0,400,600];
vista3D = [[UIView alloc] initWithFrame:area ];

[self.view addSubview:vista3D];
[vista3D release];

transformed = [CALayer layer];
transformed.frame = area;
[vista3D.layer addSublayer:transformed];

CALayer *imageLayer = [CALayer layer];
imageLayer.doubleSided = YES; 

imageLayer.frame = area;
imageLayer.transform = CATransform3DMakeRotation(40 * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);

imageLayer.contents = (id)myRawImage.CGImage;

[transformed addSublayer:imageLayer];

// Add a perspective effect
CATransform3D initialTransform = transformed.sublayerTransform;
initialTransform.m34 = 1.0 / -500;
transformed.sublayerTransform = initialTransform;

// now the layer is in perspective
// my next step is to "flatten" the composition into a UIImage

UIImage *thisIsTheResult = some magic command

thanks for any help!

EDIT 1: I have tried jessecurry solution but it gives me a flat layer without any perspective.

EDIT 2: I discovered a partial solution for this that works, but this solution gives me an image the size of the screen and I was looking for obtaining a higher resolution version, rendering off screen.


回答1:


Within the confines of the iPhone SDK, there is no way to do this. You can grab the contents of the screen with UIGetScreenImage or use -[CALayer renderInContext:] to have a layer draw into a CGContext, but forcing the GPU to composite only a subset of layers into a buffer is something that can only be achieved using private APIs and methods.




回答2:


Try this:

UIGraphicsBeginImageContext( myView.bounds.size );
[myView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();



回答3:


In addition to @jessecurry's suggestions, have you tried this?

UIGraphicsBeginImageContext( myView.bounds.size );
[[myView.layer presentationLayer] renderInContext: UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Basically applying the same idea but on the presentatioLayer. This is supposed to return the layer with the any animations and properties applied to it.




回答4:


Always open your CGContext with the new UIGraphicsBeginImageContextWithOptions function, as follow:

CGSize visibleArea = self.view.bounds.size;
CGFloat myContentScaleFactor = effect.contentScaleFactor; //this property is 2.0 for some devices

UIGraphicsBeginImageContextWithOptions(visibleArea, NO, myContentScaleFactor);
[some.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This works for me (iPhone3G, 3GS, 4).




回答5:


If you want higher resolution then you'll need to scale up the CGContext



来源:https://stackoverflow.com/questions/2289948/iphone-is-it-impossible-to-grab-the-contents-of-a-calayers-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!