我可以用我创建的CGContextRef调用CALayer的renderInContext吗?

时间:2017-06-26 06:11:28

标签: ios calayer

如果我使用来自'UIGraphicsGetCurrentContext'的上下文和CALayer的renderInContext接口,我可以正确获取UIView的快照,但是如果我使用'CGBitmapContextCreate'创建的上下文,则快照图像会被镜像并旋转180度,甚至这两个上下文具有相同的'width','height','bitmap info'(8198,表示'kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little')和'color space'(等于CGColorSpaceCreateDeviceRGB())。

代码正确运行:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height), YES, 1);  
[view.layer renderInContext:UIGraphicsGetCurrentContext()];  
UIGraphicsEndImageContext();

代码获得镜像并旋转图像:

CGContextRef context = 0;  
CGColorSpaceRef colorSpace;  
size_t width = view.bounds.size.width;  
size_t height = view.bounds.size.height;  
size_t bitsPerPixel = 32;  
size_t bitsPerComponent = 8;  
size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;  
size_t bytesPerRow = width * bytesPerPixel;  
colorSpace = CGColorSpaceCreateDeviceRGB();  
uint32_t *buffer = new uint32_t[width * height];  
context = CGBitmapContextCreate(buffer,  
                                     width,  
                                     height,  
                                     bitsPerComponent,  
                                     bytesPerRow,  
                                     colorSpace,  
                                     kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);  
CGColorSpaceRelease(colorSpace);  
[view.layer renderInContext:context]; 

0 个答案:

没有答案