如果我使用来自'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];