【问题标题】:CGImageRef not init/alloc correctlyCGImageRef 没有正确初始化/分配
【发布时间】:2015-12-13 03:58:13
【问题描述】:

我目前遇到CGImageRef 的问题。

每当我创建一个 CGImageRef 并在调试器视图中查看它时,在 Xcode 中,它是 nil

代码如下:

-(void)mouseMoved:(NSEvent *)theEvent{
    if (self.shoulddrag) {
        NSPoint event_location = [theEvent locationInWindow];//direct from the docs
        NSPoint local_point = [self convertPoint:event_location fromView:nil];//direct from the docs

        CGImageRef theImage = (__bridge CGImageRef)(self.image);
        CGImageRef theClippedImage = CGImageCreateWithImageInRect(theImage, CGRectMake(local_point.x,local_point.y,1,1));

        NSImage * image = [[NSImage alloc] initWithCGImage:theClippedImage     size:NSZeroSize];

        self.pixleView.image = image;
        CGImageRelease(theClippedImage);
    }
}

其他一切似乎都在工作。我无法理解。任何帮助将不胜感激。

注意:self.pixelView 是一个 NSImageView 实例,未被以任何方式覆盖。

【问题讨论】:

    标签: objective-c nsimage cgimageref


    【解决方案1】:

    很可能local_point 不在图像内。您已将点从窗口转换为视图坐标,但这可能不等于图像坐标。对此进行测试以查看图像的左下角是否导致 local_point(0,0)

    目前尚不清楚您的视图是如何布局的,但我怀疑您想要做的是减去用户正在与之交互的任何区域(可能是子视图)相对于self 的原点。

    【讨论】:

    • 是的,我知道local_point 有问题。但是,我已经尝试过使用(0,0,1,1) 而不是(local_point.x, local_point.y, 1, 1)。它仍然没有工作
    【解决方案2】:

    好的,我想通了。


    我用来创建 CGImageRef 的是:

        CGImageRef theImage = (__bridge CGImageRef)(self.image);
    

    显然我应该使用的是:

        CGImageSourceRef theImage = CGImageSourceCreateWithData((CFDataRef)[self.image TIFFRepresentation], NULL);
    

    我想我的问题是,出于某种原因,我认为 NSImageCGImageRef 有免费桥接。

    看来我错了。

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多