如何以编程方式更改 UIImage 的色调? [英] How to programmatically change the hue of UIImage?

查看:22
本文介绍了如何以编程方式更改 UIImage 的色调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对图像处理很陌生.我必须在我的 iPhone 应用程序项目中实现色调效果.因此,我需要更改 UIImage 的色调.请为此提供任何示例代码或教程链接.

I am very new to the image processing. I have to implement hue effect in my iPhone application project. Therefore, I need to change the hue of UIImage. Please provide me any sample code or tutorial link for this.

提前致谢

推荐答案

首先,您需要决定是将图像设为固定色调,还是旋转现有色调em>.下面显示了两者的示例.

First, you need to decide if you are making an image a fixed hue, or rotating the existing hue. Examples of both are shown below.

可以使用标准绘图工具修复色调 - 只需确保将混合模式设置为 kCGBlendModeHue,并使用适当的色调进行绘制.这是源自 Nitish 的解决方案,尽管我发现使 saturation 小于 1.0 会产生不一致的结果.这种方法确实允许改变 alpha,但对于 100% 以外的真正饱和度,您可能需要进行第二轮绘制.

Fixing the hue can be done using the standard drawing tools - just make sure the blend mode is set to kCGBlendModeHue, and draw with the appropriate hue. This is derived from Nitish's solution, although I found that making saturation less than 1.0 had inconsistent results. This method does allow varying the alpha, but for true saturation other than 100% you will probably need a second round of drawing.

- (UIImage*) imageWithImage:(UIImage*) source fixedHue:(CGFloat) hue alpha:(CGFloat) alpha;
// Note: the hue input ranges from 0.0 to 1.0, both red.  Values outside this range will be clamped to 0.0 or 1.0.
{
    // Find the image dimensions.
    CGSize imageSize = [source size];
    CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);

    // Create a context containing the image.
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    [source drawAtPoint:CGPointMake(0,0)];

    // Draw the hue on top of the image.
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
    [imagePath fill];

    // Retrieve the new image.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return result;
}

旋转色调

要旋转色调,您需要 Core Image 滤镜.下面的代码将 UIImage 转换为 CIImage,然后将结果转换回 UIImage 进行显示.根据您图片的来源,您或许可以避免其中一个或两个步骤.

Rotated hue

To rotate the hue, you need Core Image filters. The code below converts the UIImage to a CIImage, and then converts the result back to UIImage for display. Depending on where your image comes from, you may be able to avoid one or both of these steps.

方便的是,核心图像编程指南:使用核心图像过滤器 使用 CIHueAdjust 过滤器.

Conveniently, the very first example in the Core Image Programming Guide: Using Core Image Filters uses the CIHueAdjust filter.

// #import <CoreImage/CoreImage.h>
// Partially from https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/CoreImaging/ci_tasks/ci_tasks.html
- (UIImage*) imageWithImage:(UIImage*) source rotatedByHue:(CGFloat) deltaHueRadians;
{
    // Create a Core Image version of the image.
    CIImage *sourceCore = [CIImage imageWithCGImage:[source CGImage]];

    // Apply a CIHueAdjust filter
    CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
    [hueAdjust setDefaults];
    [hueAdjust setValue: sourceCore forKey: @"inputImage"];
    [hueAdjust setValue: [NSNumber numberWithFloat: deltaHueRadians] forKey: @"inputAngle"];
    CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];

    // Convert the filter output back into a UIImage.
    // This section from http://stackoverflow.com/a/7797578/1318452
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];
    UIImage *result = [UIImage imageWithCGImage:resultRef];
    CGImageRelease(resultRef);

    return result;
}

这篇关于如何以编程方式更改 UIImage 的色调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆