如何以编程方式更改UIImage的色调? [英] How to programmatically change the hue of UIImage?
问题描述
我对图像处理很新。我必须在我的iPhone应用程序项目中实现hue效果。因此,我需要更改 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.
提前致谢
推荐答案
首先,您需要确定是将图像设为固定色调,还是旋转现有色调。两者的示例如下所示。
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的解决方案中得出的,虽然我发现饱和度
小于 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屋!