我是很新的图像处理。 我要实现我的iPhone应用程序项目色调效果。 因此,我需要改变的色调UIImage
。 请给我一些示例代码或教程链接这一点。
提前致谢
我是很新的图像处理。 我要实现我的iPhone应用程序项目色调效果。 因此,我需要改变的色调UIImage
。 请给我一些示例代码或教程链接这一点。
提前致谢
首先,你需要决定如果你正在图像的色相固定 ,或旋转现有的色调 。 两者的例子如下所示。
固定色调可以使用标准的绘图工具来实现-只要确保混合模式设置为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;
}
要旋转的色调,你需要核心的图像过滤器。 下面的代码的的UIImage转换为CIImage,然后将结果转换回的UIImage用于显示。 根据您的影像来源,你可能能够避免一个或两个的这些步骤。
方便的是,在第一个示例核心绘图编程指南:使用核心图像过滤器使用CIHueAdjust过滤器。
// #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;
}
- (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();
}
服用Dondragmer的出色的固定色调例如作为起点。 下面是我修改,修复透明区域被设置与同色调的问题。
- (UIImage*) imageWithImage:(NSString *)name 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.
{
UIImage *source = [UIImage imageNamed:name];
// 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)];
// Setup a clip region using the image
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, source.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, imageExtent, source.CGImage);
//[[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
CGContextFillRect(context, imageExtent);
// Draw the hue on top of the image.
CGContextDrawImage(context, imageExtent, source.CGImage);
CGContextSetBlendMode(context, kCGBlendModeHue);
[[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
[imagePath fill];
CGContextRestoreGState(context); // remove clip region
// Retrieve the new image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
谢谢:
如何改变具有透明区域的UIImage的色调?
CGContextClipToMask UIImage的面膜反转向上和向下
在苹果公司的斯威夫特的博客,我碰到这个照片过滤应用教程来了: https://developer.apple.com/swift/blog/?id=16
以下是编辑以创建返回应用了随机色调一个UIImage一个包含方法的代码示例。
func applyHue(source: UIImage) -> UIImage {
// Create a place to render the filtered image
let context = CIContext(options: nil)
// Create an image to filter
let inputImage = CIImage(image: source)
// Create a random color to pass to a filter
let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]
// Apply a filter to the image
let filteredImage = inputImage!.imageByApplyingFilter("CIHueAdjust", withInputParameters: randomColor)
// Render the filtered image
let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent)
// Return a UIImage
return UIImage(CGImage: renderedImage)
}
的Dondragmer的旋转编码斯威夫特以上版本
// Convert the filter output back into a UIImage.
// This section from http://stackoverflow.com/a/7797578/1318452
func imageWithImage(source: UIImage, rotatedByHue: CGFloat) -> UIImage {
// Create a Core Image version of the image.
let sourceCore = CIImage(CGImage: source.CGImage)
// Apply a CIHueAdjust filter
let hueAdjust = CIFilter(name: "CIHueAdjust")
hueAdjust.setDefaults()
hueAdjust.setValue(sourceCore, forKey: "inputImage")
hueAdjust.setValue(CGFloat(rotatedByHue), forKey: "inputAngle")
let resultCore = hueAdjust.valueForKey("outputImage") as CIImage!
let context = CIContext(options: nil)
let resultRef = context.createCGImage(resultCore, fromRect: resultCore!.extent())
let result = UIImage(CGImage: resultRef)
return result!
}