简体   繁体   English

在UIImageView中更改图像的背景颜色

[英]Changing background color of Image in UIImageView

In one of my ViewController, i have an image "white background" in ImageView, and i need to be able to change the image background color on the fly. 在我的一个ViewController中,我在ImageView中有一个图像“白色背景”,我需要能够即时更改图像背景颜色。 I'm not sure if its even possible changing only the image background color NOT the background color of ImageView. 我不确定是否甚至可能仅更改图像背景颜色而不更改ImageView的背景颜色。 The other option i see is to have multiple images of different color and change images on the fly? 我看到的另一个选择是拥有多张不同颜色的图像并即时更改图像? is there are a better way to handle this situation? 有没有更好的方法来处理这种情况? Thanks in Advance. 提前致谢。

I don't think you can change the background color of an image. 我认为您无法更改图像的背景色。 You can change the background color of the UIImageView as you said. 您可以按照您所说的那样更改UIImageView的背景颜色。 Maybe you can have multiple images with different colors and load them according to your requirement. 也许您可以拥有多个具有不同颜色的图像,然后根据需要加载它们。

I would probably do it the way you were saying. 我可能会按照您所说的去做。 Just have multiple images and change them programmatically. 只需拥有多个图像并以编程方式更改它们即可。 I don't believe there is a simple way to edit images the way you are wanting. 我不相信有一种简单的方法可以按照您想要的方式编辑图像。

You'll need to use OpenGL. 您需要使用OpenGL。 I used a game framework called Sparrow for this purpose. 为此,我使用了一个名为Sparrow的游戏框架。 Sparrow is an OpenGL wrapper for making iOS games. Sparrow是用于制作iOS游戏的OpenGL包装器。 I have several white "tiles" that I programatically changed to different colors. 我有几个白色的“平铺”,我通过编程将其更改为不同的颜色。 It was the opposite of what you want, in that you want the "background" to be changed where I was changing the "foreground". 这与您想要的相反,因为您希望在我更改“前景”的地方更改“背景”。 That said you could probably use a mask image to achieve this desired effect. 就是说,您可能可以使用遮罩图像来达到所需的效果。

The great thing is that you can just pile different images on top of eachother in a SPSprite object. 很棒的是,您可以将不同的图像堆叠在一个SPSprite对象中。 You could create a white SPQuad object as the "rear" object in the sprite, and then use setColor:ofVertex: to change the SPQuad's color, leaving the image in the foreground intact, provided that it has the associated transparency in the right places. 您可以在sprite SPQuad白色SPQuad对象创建为“后”对象,然后使用setColor:ofVertex:更改SPQuad的颜色,并保持前景中的图像完整,前提是它在正确的位置具有关联的透明度。

The class involved is called SPImage and the method called is setColor:ofVertex: (inherited from SPQuad ) 涉及的类称为SPImage ,方法名为setColor:ofVertex:SPQuad继承)

The only downside to this is that you'll have to rewrite your application because the OpenGL layer takes the top level and you need to call a special function to display iOS controls like Navigation Controllers and buttons. 唯一的缺点是您必须重写应用程序,因为OpenGL层处于顶层,并且您需要调用特殊功能来显示iOS控件,例如导航控制器和按钮。 It's meant to be the guts to an entirely custom project. 这是完全定制项目的胆量。

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

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