iphone - 如何以编程方式更改 UIImage 的色调?

标签 iphone objective-c image-processing hue

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

提前致谢

最佳答案

首先,您需要决定是将图像设为固定色调,还是旋转现有色调。两者的示例如下所示。

Hue changes

固定色调

可以使用标准绘图工具修复色调 - 只需确保将混合模式设置为 kCGBlendModeHue,然后使用适当的色调进行绘制。这是源自 Nitish 的解决方案,尽管我发现使 saturation 小于 1.0 会产生不一致的结果。此方法确实允许改变 alpha,但对于 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;
}

旋转色调

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

方便的是,Core Image Programming Guide: Using Core Image Filters 中的第一个示例使用 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;
}

关于iphone - 如何以编程方式更改 UIImage 的色调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555071/

相关文章:

使用 spritekit 的 iPad 和 iPhone 游戏的 iOS 坐标

iphone - 在sqlite中使用行id比较两个表

ios - 桥接头文件找不到导入的 Objective-C 项目

image-processing - 使用 ImageMagick 压缩 JPG 文件的建议

image - 如何在 matlab 或其他方式中执行颜色量化

image-processing - 如何在 Swift for iOS 中加载和编辑像素级别的位图文件?

iOS 数据存储问题 - 即使在 NSURLIsExcludedFromBackupKey 之后也被拒绝

iphone - 如何屏蔽 UIView

objective-c - 如何将可变数量的参数传递给可变参数函数

objective-c - NSPopUpButton 所选项目的整数值