(iphone)灰度图像太暗,我可以更改灰度图像的不透明度吗?

(iphone)灰度图像太暗,我可以更改灰度图像的不透明度吗?,iphone,image,grayscale,Iphone,Image,Grayscale,我使用以下代码将彩色图像转换为灰度图像 生成的图像为灰色,但太暗。 我可以改变它的不透明度吗?(不确定术语“不透明性”是否正确) 仅转换为灰度有时会导致图像太暗,因为RGB->灰度转换可能无法保持图像的(感知亮度)。您有两种选择:(1)在转换为灰度后使图像变亮(您可以尝试);以及(2)转换保持亮度的图像 在保持亮度的同时,从RGB转换为灰度的一种常见方法是根据以下函数设置灰度值(Y): Y=0.30 x红色+0.59 x绿色+0.11 x蓝色 这是因为人眼感知到给定强度的绿色比相同强度的红色或蓝

我使用以下代码将彩色图像转换为灰度图像

生成的图像为灰色,但太暗。
我可以改变它的不透明度吗?(不确定术语“不透明性”是否正确)


仅转换为灰度有时会导致图像太暗,因为RGB->灰度转换可能无法保持图像的(感知亮度)。您有两种选择:(1)在转换为灰度后使图像变亮(您可以尝试);以及(2)转换保持亮度的图像

在保持亮度的同时,从RGB转换为灰度的一种常见方法是根据以下函数设置灰度值(Y):

Y=0.30 x红色+0.59 x绿色+0.11 x蓝色

这是因为人眼感知到给定强度的绿色比相同强度的红色或蓝色“更亮”(约为该比例)

+ (UIImage *)convertImageToGrayScale: (UIImage*) image
{
    // Create image rectangle with current image width/height                                                                                                                                                                               
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

    // Grayscale color space                                                                                                                                                                                                                
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    // Create bitmap content with current image size and grayscale colorspace                                                                                                                                                               
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

    // Draw image into current context, with specified rectangle                                                                                                                                                                            
    // using previously defined context (with grayscale colorspace)                                                                                                                                                                         
    CGContextDrawImage(context, imageRect, [image CGImage]);

    // Create bitmap image info from pixel data in current context                                                                                                                                                                          
    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    // Create a new UIImage object                                                                                                                                                                                                          
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];

    // Release colorspace, context and bitmap information                                                                                                                                                                                   
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);

    // Return the new grayscale image                                                                                                                                                                                                       
    return newImage;
}