用编程的方式更改uiimage色调
首先,你需要决定,如果你正在一个固定的色调图像,还是旋转现有的色调
Fixed hue 固定色调
固定的色调,可以使用标准的绘图工具-只需要确保的混合模式设置kCGBlendModeHue ,合适的色调与绘制。
这是来自尼蒂什的解决方案,但我发现, saturation小于1.0有不一致的结果。
这种方法是允许不同的字母,但真正的饱和度超过100%,你可能会需要第二轮的绘图
- (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; }
Rotated hue旋转色调
要旋转的色调,你需要的Core Image过滤器
下面的代码转换的UIImage到一个CIImage,然后将结果转换的UIImage显示。
根据您的图片来自,您可能能够避免这些步骤中的一个或两个
.方便的是,第一个例子中的的核心图像编程指南:核心的图像过滤器使用的CIHueAdjust的过滤器。
// #import <CoreImage/CoreImage.h>
- (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; }
2
- (void) changeToHue:(float)hue saturation:(float)saturation
{ UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIView *hueBlend = [[UIView alloc] initWithFrame:self.bounds];
hueBlend.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:1 alpha:1];
CGContextDrawImage(context, self.bounds, self.image.CGImage);
CGContextSetBlendMode(context, kCGBlendModeHue);
[hueBlend.layer renderInContext:context];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); }
posted on 2013-07-06 16:22 luoyajunios 阅读(229) 评论(0) 收藏 举报
浙公网安备 33010602011771号