0

If I use the context that got from 'UIGraphicsGetCurrentContext' with CALayer's renderInContext interface I can get the UIView's snapshot correctly, but if I use the context I created by 'CGBitmapContextCreate' the snapshot image is mirrored and rotated by 180 degree, even the two context has the same 'width','height', 'bitmap info'(8198, which means 'kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little') and 'color space'(which is equal to CGColorSpaceCreateDeviceRGB()).

The code runs correctly:

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

The code get mirrored and rotated image:

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 Answers0