简体   繁体   English

使用Core Graphics发生意外的图像颜色变化

[英]Unexpected image color change using Core Graphics

Following a general image creation and manipulation code suggestion in Add transparent space around a UIImage I ran into trouble from colors changing away from expected values. 遵循在UIImage周围添加透明空间中的一般图像创建和处理代码建议之后,我遇到了颜色从期望值更改为零的麻烦。

(Please note that while I have simplified the code included herein such that the button is now the same size as the image, I was using the image manipulation code for a situation where the button is wider than the image, and placing that smaller image to the right end of the button's region. I had no trouble in that placement, but noted the color problem, so in creating this example, I reduced the button to the same size as the image, but retained the code that is evidently responsible for the undesirable color change.) (请注意,虽然我已经简化了本文中包含的代码,以使按钮现在与图像的大小相同,但我是在按钮比图像宽的情况下使用图像处理代码,并将该较小的图像放置在我在该位置上没有问题,但是注意到了颜色问题,因此在创建此示例时,我将按钮缩小为与图像相同的大小,但保留了明显负责代码的代码。不良的颜色变化。)

To reproduce the problem, start with a standard Xcode Game template project, in ObjC, using Metal. 要重现该问题,请使用Metal在ObjC中从标准Xcode Game模板项目开始。 All of the following changes are made in GameViewController.m, mostly to the method viewDidLoad. 在GameViewController.m中进行了以下所有更改,主要是对viewDidLoad方法的更改。

Start by commenting out the last 3 lines in viewDidLoad, which deal with the Renderer. 首先注释掉viewDidLoad中与渲染器有关的最后3行。 This removes the rotating cube from the display, making the situation I'm detailing easier to see. 这将从显示中删除旋转的多维数据集,使我正在详细说明的情况更容易看到。

Then add the following code in its place... 然后在其位置添加以下代码...

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(350.0, 100.0, 90.0, 50.0);
[_view addSubview:button];

UIGraphicsBeginImageContextWithOptions(CGSizeMake(90.0, 50.0), NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);

UIImage *oldImage = [UIImage imageNamed:@"SquiggleBox_90x50_01.png"];
//UIImage *oldImage = [UIImage imageNamed:@"SquiggleBox_90x50_01.jpg"];
[oldImage drawAtPoint:CGPointMake(0.0, 0.0)];

UIGraphicsPopContext();
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[button setImage:newImage forState:UIControlStateNormal];

(I have provided the two image files below. The first is a .PNG, the second is a .JPG, both 90x50 pixels. Both show a gold squiggle (a thin line that wanders the interior portion of the image) in a gold outline (a thin gold line on the rectangular border of the image. The PNG file has transparency where there is no gold color, the JPG uses a white fill. The PNG was created with Adobe Illustrator, and the JPG was converted via Photoshop. Save them with the names given below, and you can proceed with the code.) (我在下面提供了两个图像文件。第一个是.PNG,第二个是.JPG,两者均为90x50像素。都以金色轮廓显示了金字形(细线在图像的内部徘徊)。 (图像矩形边框上的金细线。PNG文件具有透明性,没有金色,JPG使用白色填充。PNG是使用Adobe Illustrator创建的,JPG是通过Photoshop转换的。保存它们(使用下面给出的名称),然后可以继续执行代码。)

SquiggleBox_90x50.png SquiggleBox_90x50.jpg

Finally, add the following two image files to the project, so that the code can reference either of them. 最后,将以下两个图像文件添加到项目中,以便代码可以引用其中两个。

SquiggleBox_90x50.png
SquiggleBox_90x50.jpg

When the PNG is used, the gold squiggle and outline turn blue. 使用PNG时,金字形和轮廓变为蓝色。 When the JPEG is used, the entirety of the button is solid blue. 使用JPEG时,按钮的整体为纯蓝色。

Kevin Enax had the important clue... tinting. 凯文·埃纳克斯(Kevin Enax)有重要的线索。 The solution is to add a single line to the code, immediately after the line defining newImage, before ending the context, put 解决方案是在定义newImage的行之后,在结束上下文之前,在代码中添加一行

newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

This prevents tinting, and the original colors come through as desired. 这样可以防止着色,并且可以根据需要呈现原始颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM