`-drawInRect` 在 10.11 中似乎有所不同?什么可以改变?
`-drawInRect` seems to be different in 10.11? What could have changed?
编辑:我得到了反对票,我只是想确定这是因为我没有问开发论坛(我问过,https://forums.developer.apple.com/thread/11593,但没有得到任何回应)。这对我们来说是一个相当大的问题,所以我认为最好把范围扩大一些,也许有人可以提供帮助。
我们有一个应用程序可以在 10.10 中运行,但在 10.11 中有这个问题。
我们在 NSGraphicsContext
中调用 -drawRect
在 NSImage
中调用 OS
但是在 10.11 中,这个 NSImage
不会被绘制。
我是新手,但我已经调试了很长时间才到达现在的位置,但我只是被困住了。之前正在查看是否有人 运行,或者知道为什么会这样。
这里是相关的代码:
(layer
对象是从 -drawRect
方法传入此方法的 CGLayerRef
对象)
以下是层的实例化方式:
NSRect scaledRect = [Helpers scaleRect:rect byScale:[self backingScaleFactorRelativeToZoom]];
CGSize size = NSSizeToCGSize(rect.size);
size_t width = size.width;
size_t height = size.height;
size_t bitsPerComponent = 8;
size_t bytesPerRow = (width * 4+ 0x0000000F) & ~0x0000000F; /
size_t dataSize = bytesPerRow * height;
void* data = calloc(1, dataSize);
CGColorSpaceRef colorspace = [[[_imageController document] captureColorSpace] CGColorSpace];
CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
CGLayerRef canvasLayer = CGLayerCreateWithContext(bitmapContext, scaledRect.size, NULL);
这里是绘制图像的方法:
CGContextRef mainLayerContext = CGLayerGetContext(layer);
NSRect scaledBounds = [contextInfo[kContextInfoBounds] rectValue];
if( !_flatCache )
{
_flatCache = CGLayerCreateWithContext(mainLayerContext, scaledBounds.size, NULL);
CGContextRef flatCacheCtx = CGLayerGetContext(_flatCache);
CGLayerRef tempLayer = CGLayerCreateWithContext(flatCacheCtx, scaledBounds.size, NULL);
CIImage *tempImage = [CIImage imageWithCGLayer:tempLayer];
NSLog(@"%@",tempImage);
CGContextRef tempLayerCtx = CGLayerGetContext(tempLayer);
CGContextTranslateCTM(tempLayerCtx, -scaledBounds.origin.x, -scaledBounds.origin.y);
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext* newContext = [NSGraphicsContext graphicsContextWithGraphicsPort:tempLayerCtx flipped:NO];
[NSGraphicsContext setCurrentContext:newContext];
if ( [_imageController background] )
{
NSRect bgRect = { [_imageController backgroundPosition], [_imageController backgroundSize] };
bgRect = [Helpers scaleRect:bgRect byScale:[self backingScaleFactorRelativeToZoom]];
[[_imageController background] drawInRect:bgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
CIImage *tempImage2 = [CIImage imageWithCGLayer:tempLayer];
NSLog(@"%@",tempImage2);
在 10.10 和 10.11 中,tempImage 是一张大小正确的空图像。
在 10.10 tempImage2 现在 [_imageController background]
正确绘制
在 10.11 中,tempImage2 与 tempImage 相同,空白图像具有正确的大小
可惜当初写这段代码的人已经不在了,我也是菜鸟,没法再往下挖了w/o找本书看了
bgRect 不是问题,已经尝试修改它。我也搞砸了 -translate
论据,但仍然无法学到任何东西。
有人知道我还能如何调试它来找到问题吗?或者更好的是,有没有人看到这个问题并知道我的问题是什么?
只需要添加这一行:
//`bounds` calculated from the size of the image
//mainLayerContext and flatCache defined above in question ^^
CGContextDrawLayerInRect(mainLayerContext, bounds, _flatCache);
最后
编辑:我得到了反对票,我只是想确定这是因为我没有问开发论坛(我问过,https://forums.developer.apple.com/thread/11593,但没有得到任何回应)。这对我们来说是一个相当大的问题,所以我认为最好把范围扩大一些,也许有人可以提供帮助。
我们有一个应用程序可以在 10.10 中运行,但在 10.11 中有这个问题。
我们在 NSGraphicsContext
中调用 -drawRect
在 NSImage
中调用 OS
但是在 10.11 中,这个 NSImage
不会被绘制。
我是新手,但我已经调试了很长时间才到达现在的位置,但我只是被困住了。之前正在查看是否有人 运行,或者知道为什么会这样。
这里是相关的代码:
(layer
对象是从 -drawRect
方法传入此方法的 CGLayerRef
对象)
以下是层的实例化方式:
NSRect scaledRect = [Helpers scaleRect:rect byScale:[self backingScaleFactorRelativeToZoom]];
CGSize size = NSSizeToCGSize(rect.size);
size_t width = size.width;
size_t height = size.height;
size_t bitsPerComponent = 8;
size_t bytesPerRow = (width * 4+ 0x0000000F) & ~0x0000000F; /
size_t dataSize = bytesPerRow * height;
void* data = calloc(1, dataSize);
CGColorSpaceRef colorspace = [[[_imageController document] captureColorSpace] CGColorSpace];
CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
CGLayerRef canvasLayer = CGLayerCreateWithContext(bitmapContext, scaledRect.size, NULL);
这里是绘制图像的方法:
CGContextRef mainLayerContext = CGLayerGetContext(layer);
NSRect scaledBounds = [contextInfo[kContextInfoBounds] rectValue];
if( !_flatCache )
{
_flatCache = CGLayerCreateWithContext(mainLayerContext, scaledBounds.size, NULL);
CGContextRef flatCacheCtx = CGLayerGetContext(_flatCache);
CGLayerRef tempLayer = CGLayerCreateWithContext(flatCacheCtx, scaledBounds.size, NULL);
CIImage *tempImage = [CIImage imageWithCGLayer:tempLayer];
NSLog(@"%@",tempImage);
CGContextRef tempLayerCtx = CGLayerGetContext(tempLayer);
CGContextTranslateCTM(tempLayerCtx, -scaledBounds.origin.x, -scaledBounds.origin.y);
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext* newContext = [NSGraphicsContext graphicsContextWithGraphicsPort:tempLayerCtx flipped:NO];
[NSGraphicsContext setCurrentContext:newContext];
if ( [_imageController background] )
{
NSRect bgRect = { [_imageController backgroundPosition], [_imageController backgroundSize] };
bgRect = [Helpers scaleRect:bgRect byScale:[self backingScaleFactorRelativeToZoom]];
[[_imageController background] drawInRect:bgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
CIImage *tempImage2 = [CIImage imageWithCGLayer:tempLayer];
NSLog(@"%@",tempImage2);
在 10.10 和 10.11 中,tempImage 是一张大小正确的空图像。
在 10.10 tempImage2 现在 [_imageController background]
正确绘制
在 10.11 中,tempImage2 与 tempImage 相同,空白图像具有正确的大小
可惜当初写这段代码的人已经不在了,我也是菜鸟,没法再往下挖了w/o找本书看了
bgRect 不是问题,已经尝试修改它。我也搞砸了 -translate
论据,但仍然无法学到任何东西。
有人知道我还能如何调试它来找到问题吗?或者更好的是,有没有人看到这个问题并知道我的问题是什么?
只需要添加这一行:
//`bounds` calculated from the size of the image
//mainLayerContext and flatCache defined above in question ^^
CGContextDrawLayerInRect(mainLayerContext, bounds, _flatCache);
最后