简体   繁体   中英

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. I'm not sure if its even possible changing only the image background color NOT the background color of 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. 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. I used a game framework called Sparrow for this purpose. Sparrow is an OpenGL wrapper for making iOS games. 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. 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.

The class involved is called SPImage and the method called is setColor:ofVertex: (inherited from 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. It's meant to be the guts to an entirely custom project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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