如果我通過CALayer的renderInContext接口使用從'UIGraphicsGetCurrentContext'獲得的上下文,我可以正確地獲取UIView的快照,但是如果使用由'CGBitmapContextCreate'創建的上下文,快照圖像將被鏡像並旋轉180度,即使兩個上下文具有相同的「寬度」,「高度」,「位圖信息」(8198,表示'kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little')和'色彩空間'(等於CGColorSpaceCreateDeviceRGB())。我可以用創建的CGContextRef調用CALayer的renderInContext嗎?
的代碼運行正常:
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];
有一篇關於同樣的事情的帖子:https://stackoverflow.com/questions/7856127/using-calayers-renderincontext-method-with-geometryflipped?rq = 1答案是'翻轉目標上下文之前你渲染它「,但是爲什麼? – XiangmeiNorth
上下文具有'ctm'屬性,不是嗎? – kelin
我沒有設置任何圖層的geometryFlipped屬性,爲什麼我應該做這個變換? – XiangmeiNorth