开发者

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

I have a CALayer transformed in 3D on a offscreen UIView (much larger t开发者_JS百科han 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.


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.


Try this:

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


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.


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).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜