“renderInContext:” and CATransform3D

左心房为你撑大大i 提交于 2019-12-03 00:46:00

have you actually tried it? I'm currently working on a project with several 3D transforms, and when I try to programmatically make this screenshot it works just fine :) Here is the code I use:

-(UIImage *)getScreenshot
{
    CGFloat scale = 1.0;
    if([[UIScreen mainScreen]respondsToSelector:@selector(scale)])
    {        
        CGFloat tmp = [[UIScreen mainScreen]scale];
        if (tmp > 1.5)
        {
            scale = 2.0;    
        }
    }      
    if(scale > 1.5)
    {
        UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale);
    } 
    else
    {
        UIGraphicsBeginImageContext(self.frame.size);
    }    
    //SELF HERE IS A UIVIEW
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];    
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return screenshot;
}

I got it working with protocols.... I'm implementing a protocol in all UIViews classes that make 3D transforms. So when I request a screenshot, it make all subviews screenshot, and generate one UIImage.. Not so good for lots of views, but I'm doing in a few views.

#pragma mark - Protocol implementation 'TDITransitionCustomTransform'

//Conforms to "TDITransitionCustomTransform" protocol, return currrent image view state , by current layer

- (UIImage*)imageForCurrentState {

//Make print

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//return printed image

return screenShot;

}

I was thinking it may works now because I'm doing that render in the transformed view layer, which have being transformed it self... And it wasn't working because "renderInContext:" doesn't get layers of it subviews, may it possible ?

Anyone interest in a bit more code of this solution, can be found here . in the apple dev forum.

It may be a function bug, or it just not being design for this purpose ...

May Be You can use Core Graphaic instead of CATransform3DMakeRotation :)

CGAffineTransform flip = CGAffineTransformMakeScale(1.0, -1.0);

Which get effet on the renderInContext

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