【问题标题】:sepia version of any NSImage任何 NSImage 的棕褐色版本
【发布时间】:2011-04-27 06:29:49
【问题描述】:

我正在寻找一种简单有效的方法来为 NSImage 中包含的任何图标赋予特殊色彩。我最初需要 id 有棕褐色图标,但如果可以使用其他颜色就更好了。

你有什么想法来实现这个目标吗?

提前感谢您的帮助,

问候,

【问题讨论】:

  • 也许this answer(虽然它适用于 iPhone)会有所帮助。

标签: objective-c cocoa nsimage


【解决方案1】:

CoreImage 有built-in filters,其中之一是CISepiaTone,您可以轻松地使用它来转换图像的颜色和其他方面。

Processing an Image 中列出了您需要遵循的步骤:

获取CIContext 对象。最简单的方法可能是询问当前的NSGraphicsContext

获取您的图像的CIImage 表示。这不能直接从NSImage 创建;您可能希望使用CGImageForProposedRect:context:hints:NSImage 转换为CGImageRef(您可以将NULL 传递给proposedRect 参数)。

创建过滤器对象,设置其值,并获取处理后的图像。

最后,在你的CIContext中绘制图像。

【讨论】:

    【解决方案2】:

    我认为您将逐像素拍摄图像并调整 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,但逻辑可以重用..另外this线程是图像处理方面最好的线程之一..一个书签..

    编辑:正如 Josh Caswell 指出的那样,Core Image 可用于创建棕褐色图像和一些图像过滤。所以更简单的方法应该是使用 core Image...阅读他的回答去做..这个方法也很好,特别是在没有coreImage框架的iphone中..

    【讨论】:

    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 2021-03-28
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多