【发布时间】:2010-07-20 11:20:15
【问题描述】:
我正在编写对黑白图像进行操作的应用程序。我通过将 NSImage 对象传递到我的方法中,然后从 NSImage 生成 NSBitmapImageRep 来做到这一点。一切正常,但速度很慢。这是我的代码:
- (NSImage *)skeletonization: (NSImage *)image
{
int x = 0, y = 0;
NSUInteger pixelVariable = 0;
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
[myHelpText setIntValue:[bitmapImageRep pixelsWide]];
[myHelpText2 setIntValue:[bitmapImageRep pixelsHigh]];
NSColor *black = [NSColor blackColor];
NSColor *white = [NSColor whiteColor];
[myColor set];
[myColor2 set];
for (x=0; x<=[bitmapImageRep pixelsWide]; x++) {
for (y=0; y<=[bitmapImageRep pixelsHigh]; y++) {
// This is only to see if it's working
[bitmapImageRep setColor:myColor atX:x y:y];
}
}
[myColor release];
[myColor2 release];
NSImage *producedImage = [[NSImage alloc] init];
[producedImage addRepresentation:bitmapImageRep];
[bitmapImageRep release];
return [producedImage autorelease];
}
所以我尝试使用 CIImage,但我不知道如何通过 (x,y) 坐标进入每个像素。这真的很重要。
【问题讨论】:
-
为什么要设置颜色?这是完全没有意义的,因为没有锁定焦点,而且你也没有用这些颜色绘制任何东西。并不是说删除它会加速你的代码,我只是想让你知道。
-
您访问每个像素的方式并不理想,我也不确定您究竟想在这里做什么;您所做的只是将图像中每个像素的颜色更改为某种颜色(不知道这是从哪里来的),但是您说此代码只是为了查看它是否有效。如果您希望我发布更好的解决方案,我需要更多信息,例如什么样的图像是命名的源图像,什么样的图像是生成的图像。我想我当然可以大大加快你的代码速度,但如果我不知道它应该做什么,那就不行了。
标签: cocoa cgimage nsimage nsbitmapimagerep