macOS Mojave - NSReadPixel 不工作
macOS Mojave - NSReadPixel is not working
我已经将 NSImageView
子类化以覆盖 mouseDown
方法来获取点击点的颜色,到目前为止它在所有 OS 版本(最多 10.13.x) 但在 10.14 上这似乎不起作用,使用 NSReadPixel
时我总是得到空颜色。下面是我的 mouseDown
方法代码。
- (void)mouseDown:(NSEvent *)theEvent{
if(!self.image)
return;
NSPoint clickedPoint = theEvent.locationInWindow;
NSPoint pointInDocumentView = [self convertPoint:clickedPoint fromView:nil];
[self lockFocus];
NSColor* colorAtClickedPoint = NSReadPixel(pointInDocumentView);
[self unlockFocus];
if(colorAtClickedPoint){
if(self.delegate){
[self.delegate colourCodeDidChange:colorAtClickedPoint];
}
}
}
请帮忙。
此方法在 10.14 中已弃用。您可以使用其他方法,如下所示。希望你的回答没有问题。
- (void)mouseDown:(NSEvent *)theEvent{
if(!self.image)
return;
NSPoint clickedPoint = theEvent.locationInWindow;
NSPoint pointInDocumentView = [self convertPoint:clickedPoint fromView:nil];
NSBitmapImageRep * bitmapImageRep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:bitmapImageRep];
[self lockFocus];
NSColor* colorAtClickedPoint = [bitmapImageRep colorAtX:pointInDocumentView.x y:pointInDocumentView.y];
[self unlockFocus];
if(colorAtClickedPoint){
if(self.delegate){
[self.delegate colourCodeDidChange:colorAtClickedPoint];
}
}
}
我已经将 NSImageView
子类化以覆盖 mouseDown
方法来获取点击点的颜色,到目前为止它在所有 OS 版本(最多 10.13.x) 但在 10.14 上这似乎不起作用,使用 NSReadPixel
时我总是得到空颜色。下面是我的 mouseDown
方法代码。
- (void)mouseDown:(NSEvent *)theEvent{
if(!self.image)
return;
NSPoint clickedPoint = theEvent.locationInWindow;
NSPoint pointInDocumentView = [self convertPoint:clickedPoint fromView:nil];
[self lockFocus];
NSColor* colorAtClickedPoint = NSReadPixel(pointInDocumentView);
[self unlockFocus];
if(colorAtClickedPoint){
if(self.delegate){
[self.delegate colourCodeDidChange:colorAtClickedPoint];
}
}
}
请帮忙。
此方法在 10.14 中已弃用。您可以使用其他方法,如下所示。希望你的回答没有问题。
- (void)mouseDown:(NSEvent *)theEvent{
if(!self.image)
return;
NSPoint clickedPoint = theEvent.locationInWindow;
NSPoint pointInDocumentView = [self convertPoint:clickedPoint fromView:nil];
NSBitmapImageRep * bitmapImageRep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:bitmapImageRep];
[self lockFocus];
NSColor* colorAtClickedPoint = [bitmapImageRep colorAtX:pointInDocumentView.x y:pointInDocumentView.y];
[self unlockFocus];
if(colorAtClickedPoint){
if(self.delegate){
[self.delegate colourCodeDidChange:colorAtClickedPoint];
}
}
}