cocoa – 使用带有geometryFlipped的CALayer的renderInContext:方法

cocoa – 使用带有geometryFlipped的CALayer的renderInContext:方法,第1张

概述我有一个CALayer(containerLayer),我想在将数据保存为平面文件之前转换为NSBitmap ImageRep. containerLayer的geometryFlipped属性设置为YES,这似乎导致了问题.最终生成的PNG文件正确呈现内容,但似乎不考虑翻转的几何体.我显然正在寻找test.png来准确地表示左边显示的内容. 下面是问题的截图和我正在使用的代码. - (NSBit 我有一个CALayer(containerLayer),我想在将数据保存为平面文件之前转换为NSBitmap ImageRep. containerLayer的geometryFlipped属性设置为YES,这似乎导致了问题.最终生成的PNG文件正确呈现内容,但似乎不考虑翻转的几何体.我显然正在寻找test.png来准确地表示左边显示的内容.

下面是问题的截图和我正在使用的代码.

- (NSBitmAPImageRep *)exportToImageRep{    CGContextRef context = NulL;    CGcolorSpaceRef colorSpace;    int bitmapByteCount;    int bitmapBytesPerRow;    int pixelsHigh = (int)[[self containerLayer] bounds].size.height;    int pixelsWIDe = (int)[[self containerLayer] bounds].size.wIDth;    bitmapBytesPerRow = (pixelsWIDe * 4);    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);    colorSpace = CGcolorSpaceCreateWithname(kCGcolorSpaceGenericRGB);    context = CGBitmapContextCreate (NulL,pixelsWIDe,pixelsHigh,8,bitmapBytesPerRow,colorSpace,kCGImageAlphaPremultiplIEdLast);    if (context == NulL)    {        NSLog(@"Failed to create context.");        return nil;    }    CGcolorSpaceRelease(colorSpace);    [[[self containerLayer] presentationLayer] renderInContext:context];        CGImageRef img = CGBitmapContextCreateImage(context);    NSBitmAPImageRep *bitmap = [[NSBitmAPImageRep alloc] initWithCGImage:img];    CFRelease(img);    return bitmap;    }

作为参考,这里是实际保存生成的NSBitmAPImageRep的代码:

NSData *imageData = [imageRep representationUsingType:NSPNGfileType propertIEs:nil];[imageData writetofile:@"test.png" atomically:NO];
解决方法 您需要在渲染之前翻转目标上下文.

用这个更新你的代码,我刚解决了同样的问题:

CGAffinetransform flipVertical = CGAffinetransformMake(1,-1,pixelsHigh);CGContextConcatCTM(context,flipVertical);[[[self containerLayer] presentationLayer] renderInContext:context];
总结

以上是内存溢出为你收集整理的cocoa – 使用带有geometryFlipped的CALayer的renderInContext:方法全部内容,希望文章能够帮你解决cocoa – 使用带有geometryFlipped的CALayer的renderInContext:方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存