gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:17:03 27 4
gpt4 key购买 nike

我对图像处理很陌生。我必须在我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com