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

混江龙づ霸主 提交于 2019-11-30 16:48:02

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

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