我正在寻找一种简单而有效的方法来给NSImage中包含的任何图标赋予特殊的色调。我最初的需要ID有棕褐色的图标,但如果可以使用其他颜色,它会更好。任何NSImage的棕褐色版本
你有什么想法做到这一点?
预先感谢您的帮助,
问候,
我正在寻找一种简单而有效的方法来给NSImage中包含的任何图标赋予特殊的色调。我最初的需要ID有棕褐色的图标,但如果可以使用其他颜色,它会更好。任何NSImage的棕褐色版本
你有什么想法做到这一点?
预先感谢您的帮助,
问候,
我想你会通过像素有取图像像素和调整RGB值,以获得效果。
-(UIImage*)makeSepiaScale:(UIImage*)image
{
CGImageRef cgImage = [image CGImage];
CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
CFDataRef bitmapData = CGDataProviderCopyData(provider);
UInt8* data = (UInt8*)CFDataGetBytePtr(bitmapData);
int width = image.size.width;
int height = image.size.height;
NSInteger myDataLength = width * height * 4;
for (int i = 0; i < myDataLength; i+=4)
{
UInt8 r_pixel = data[i];
UInt8 g_pixel = data[i+1];
UInt8 b_pixel = data[i+2];
int outputRed = (r_pixel * .393) + (g_pixel *.769) + (b_pixel * .189);
int outputGreen = (r_pixel * .349) + (g_pixel *.686) + (b_pixel * .168);
int outputBlue = (r_pixel * .272) + (g_pixel *.534) + (b_pixel * .131);
if(outputRed>255)outputRed=255;
if(outputGreen>255)outputGreen=255;
if(outputBlue>255)outputBlue=255;
data[i] = outputRed;
data[i+1] = outputGreen;
data[i+2] = outputBlue;
}
CGDataProviderRef provider2 = CGDataProviderCreateWithData(NULL, data, myDataLength, NULL);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider2, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef); // YOU CAN RELEASE THIS NOW
CGDataProviderRelease(provider2); // YOU CAN RELEASE THIS NOW
CFRelease(bitmapData);
UIImage *sepiaImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // YOU CAN RELEASE THIS NOW
return sepiaImage;
}
代码被无耻地从这个SO线程复制,并且它谈论施加棕褐色过滤器的UIImage代替NSImage中,但逻辑可以reused..Also this线程是最好的一个,当涉及到图像处理..One书签..
编辑:由于乔希卡斯威尔指出,核心图像可以用来创建棕褐色图像和某些图像filtering..So更简单的方法应该是使用核心图片...阅读他的回答如何做到这一点。这种方法也很好,尤其是在没有coreImage框架的iPhone中。
有没有必要手工完成。 CoreImage不仅仅是任务。 – 2011-04-27 08:00:52
正确Josh ..我不熟悉MAC开发。编辑我的答案..这是一个可惜的CoreImage是不存在的iPhone .. – Krishnabhadra 2011-04-27 08:25:21
现在的核心图像是在iPhone上.. https://developer.apple.com/library/ ios /#documentation/graphicsimaging/Conceptual/CoreImaging/ci_intro/ci_intro.html – Krishnabhadra 2012-04-24 05:30:29
CoreImage有built-in filters,其中之一是CISepiaTone,您可以轻松地使用它来转换图像的颜色和其他方面。
您需要按照Processing an Image被布局的步骤:
得到一个CIContext
对象。最简单的方法可能是询问当前的NSGraphicsContext
。
获取图像的CIImage表示形式。这不能直接从NSImage
创建;您可能需要使用CGImageForProposedRect:context:hints:
将NSImage
转换为CGImageRef
(您可以通过NULL
获取proposedRect
参数)。
创建滤镜对象,设置其值并获取处理后的图像。
最后,在您的CIContext
中绘制图像。
也许[这个答案](http://stackoverflow.com/questions/1117211/how-would-i-tint-an-image-programatically-on-the-iphone/1118005#1118005)(虽然它是为iPhone)将有所帮助。 – 2011-04-27 06:49:09