ios - 是否可以在UIImage的部分部分添加灰度效果?

标签 ios uiimage effect grayscale

谁能告诉我如何在矩形图像上添加灰度效果? 我找到了 Convert image to grayscale ,它可以将图像转换为灰度。 但我只需要将 UIImage 的一部分转换为灰度。可能吗?

看看你的帮助 谢谢, 辉

最佳答案

我修改了其他主题中的代码以应用于您图像中的矩形。

typedef enum {
    ALPHA = 0,
    BLUE = 1,
    GREEN = 2,
    RED = 3
} PIXELS;

- (UIImage *)convertToGrayscale:(UIImage *) originalImage inRect: (CGRect) rect{
    CGSize size = [originalImage size];
    int width = size.width;
    int height = size.height;

    // the pixels will be painted to this array
    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    // clear the pixels so any transparency is preserved
    memset(pixels, 0, width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create a context with RGBA pixels
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, 
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

    // paint the bitmap to our context which will fill in the pixels array
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [originalImage CGImage]);

    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

            if(x > rect.origin.x && y > rect.origin.y && x < rect.origin.x + rect.size.width && y < rect.origin.y + rect.size.height) {

                // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale

                uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];

                // set the pixels to gray in your rect

                rgbaPixel[RED] = gray;
                rgbaPixel[GREEN] = gray;
                rgbaPixel[BLUE] = gray;
            }
        }
    }

    // create a new CGImageRef from our context with the modified pixels
    CGImageRef image = CGBitmapContextCreateImage(context);

    // we're done with the context, color space, and pixels
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);

    // make a new UIImage to return
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];

    // we're done with image now too
    CGImageRelease(image);

    return resultUIImage;
}

您可以在 UIImageView 中测试它:

imageview.image = [self convertToGrayscale:imageview.image inRect:CGRectMake(50, 50, 100, 100)];

关于ios - 是否可以在UIImage的部分部分添加灰度效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11308111/

相关文章:

iphone - 在iOS中选择单元格时需要在UITableView的行中启用UITextField

ios - 更新到 Xcode 7 Beta 5 和 Swift 2 产生了多个错误

iOS - 如何以编程方式设置/更改/重置应用程序图标?

ios - 快速创建网页缩略图

ios - XCTAssertEqual 用于 Swift 中的自定义对象

ios - 如何在使用苹果功能登录时使用 "user"参数获取电子邮件、姓名等用户信息?

调用 nil 后 iPhone 内存没有释放

.net - 使用效果时在 WPF 中呈现问题

Javascript/JQuery 做翻转效果确认

audio - 如何在 javafx 中实现 3D 环绕声和混响等音频效果