如何更改具有透明区域的 UIIMAGE 的色调? [英] How to change hue of UIIMAGE having transparent area?
本文介绍了如何更改具有透明区域的 UIIMAGE 的色调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用以下代码进行色调更改:
I am currently using following code for hue change:
CGSize imageSize = [imageView.image size];
CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);
// Create a context containing the image.
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawAtPoint:CGPointMake(0, 0)];
// Draw the hue on top of the image.
CGContextSetBlendMode(context, kCGBlendModeHue);
[color set];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
[imagePath fill];
// Retrieve the new image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
imageView.image= result;
UIGraphicsEndImageContext();
此代码运行良好,唯一的问题是它还设置了透明部分的颜色.我可以避免将色调应用于 UIImage 的透明区域吗?
This code is working fine, only issue is it also sets the colour of transparent portions. Can I avoid applying the hue to that transparent area of the UIImage?
附上以下两张图片供参考:
Attached following two images for reference:
提前致谢?
输出
推荐答案
在添加颜色之前应用图像的蒙版.
Apply a mask from the image before adding the color.
CGSize imageSize = [imageView.image size];
CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);
// Create a context containing the image.
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawAtPoint:CGPointMake(0, 0)];
// Setup a clip region using the image
CGContextSaveGState(context);
CGContextClipToMask(context, imageExtent, imageView.image.CGImage);
[color set];
CGContextFillRect(context, imageExtent);
// Draw the hue on top of the image.
CGContextSetBlendMode(context, kCGBlendModeHue);
[color set];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
[imagePath fill];
CGContextRestoreGState(context); // remove clip region
// Retrieve the new image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
imageView.image= result;
UIGraphicsEndImageContext();
这篇关于如何更改具有透明区域的 UIIMAGE 的色调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文