Alpha blending on the edges of an UIImage - iphone

I have two rectangular images: one is foreground, the other one is background. I am trying to blend the edges of the foreground image so that the foreground image looks like it is "part" of the background image. In other words, I am trying to apply a transparency effect, with the opacity of the foreground image decreasing from 100% at the center of the image down to 0% at the edges. I have found that this operation is sometimes referred by different names, such as: alpha compositing, alpha blending, edge feathering or edge transparency. Here is a more detailed description of the effect I am trying to obtain: http://en.wikipedia.org/wiki/Alpha_compositing
I have looked at the CGContext documentation, but I haven't found any function that would do that out of the box.
Is there any way to do that using CGContext or even OpenGL? Would there be a way to do it on a non-rectangular image? I know, all images are rectangular, but I mean an image with let's say, a circle in it, and a transparent area all around.

The CGImageCreateWithMask() function in the iOS library can do alpha blending with decreasing opacity on the edges. It's possible to get something to work by combining the code in these two links:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
Creating mask with CGImageMaskCreate is all black (iphone)
The level of gray (from black to white) of each pixel in the mask will define the level of opacity during blending.

Related

Incorrect white matte behind antialiasing on imported sprites

I'm importing a sprite into Unity, and adding it to a Screen Space Overlay canvas to use for a UI.
The image I'm importing looks exactly as I want it, but in Unity the anti-aliased edges look like they're going to a white background color, instead of just fading over whatever is actually behind them.
I'm using these import settings:
I'm using a default UI/Image component to add it to the canvas.
This is the image I'm importing - it's a 32 bit PNG exported from Fireworks: (also shown over a black background)
Just to confirm, this looks fine everywhere else in Unity, preview panels, pickers etc. I am packing this sprite using the built in Sprite Packer if that changes anything.
And the final result:
How can I get rid of these artifacts on the corners?
The problem was the RGB values of the transparent pixels. By default they are white, and any scaling operations cause this white color to be blended with the partially transparent pixels.
I essentially made a slightly larger version of the button background shape, put it in a layer behind everything, and then wrote back into the alpha channel making it transparent. This means that the neighboring pixels are then the same color as the partially transparent ones.
The end result:

Edge blending of image with alpha on iOS after masking color

I have an image that has been masked with a range of colors using
CGImageCreateWithMaskingColors()
Everything works fine, but the edge of the image (we are using a green screen for profile pictures) is pixelated. I've tried drawing the image in an image context and a bitmap context with the proper antialiasing, but the edges remain the same.
Any suggestions on how to smooth the sides of the profile after it has been masked?
You could try zooming the mask image larger by some multiple (say 2X, 3X or 4X) before creating the mask. Then a size-reduced copy of this larger mask might be better antialiased.

How to create an art asset that can be dynamically colored in software?

I asked this question on the Graphic Design site, but it includes a programming component that might be better answered here.
Specifically, I have a bunch of photographic crayon images. I would like to remove the color from one to produce a neutral image that I can load into an iPhone app that I'm writing and dynamically color. The crayon images have dark regions (shadows) and light regions (shine) which I would like to preserve. I will be dynamically coloring it with many different colors, ranging from white to rainbow colors to black.
My first inclination is to turn the image into a grayscale image and then somehow turn the color channel into an alpha channel, and change the color of all pixels to black. Then I could use it as a mask. However, this would only preserve the shadows, and I would lose all the highlights.
Any ideas?
Two options come to mind:
Make a grayscale version that could be tinted as you said, with the shadows and highlights simply white and gray.
Make an outline, i.e. an image with alpha that had 0% opacity in the colored parts, say 10% white over the highlights, 10% black on the shadows, and 100% black/dark gray for the lines/edges. The idea being that you could put any color under the outline and it would look right.

How to draw a light effect over a texture on iPhone using UIKit/Quartz

I have a scene with a background image (a lit room), and a black image (shadow) over that. I need to be able to move my finger over the background and reveal some parts of the scene, simulating a dim light source in a dark room.
My current approach was to generate a mask depending on the position of the touch, and then applying that mask to the shadow image. The problem is I'm generating a new mask and applying it every time I receive a touch event. It's a large image (800x600) and this causes the performance to go down and it increases a lot the memory usage, eventually crashing the game (I think I don't have any memory leaks, but that's not warrantied... anyway the performance itself isn't acceptable).
Can anyone think of a better approach (which doesn't involve using OpenGL ES -- that's not an option in this project) to do this?
To go with my comments above.
Maybe to get around the different shadow levels you could also have a grid of views (squares) between the image and the shadow view. each grid square has a different alpha opacity and when the spot is over a grid square, the grid square's alpha opacity changes to 0. when the spot moves off the grid square it's alpha opacity changes back to it's default.
Without more information it is a little difficult to know whether this approach will work in your case but what you could do is generate a single mask image, say, a radial alpha gradient and then apply an affine transform to it to shape it according to the touches. This can be used to simulate a torch/flashlight beam.
I would try this: use one view with a custom drawRect implemetation: first draw the shadow image (in grayscale) then a bright spot image in white an alpha. And finally the background image in a 'multiply' blend mode.
Just a thought, does the shadow has to be an image? Perhaps you could simply fill the shadow layer with a color and mask it then? This way the memory usage should be less and the effect should be nearly identical (if not exactly the same).
There is no reason to generate a new mask on every touch move. Instead, let the mask be initialized once and manipulate it (reset it's frame) as needed upon touch events.

How can I 'shine' a png on the iPhone in the same way that the app launcher does?

I want to show users what their square flat .png image will look like when converted to a normal 'shined' icon by the app launcher.
e.g. round corners and glassy effect.
Thanks
For in-application representation of the 'shine' on an icon, you can create a custom UIView that draws the shine gradient, using the code here (adjusting the gradient colors to match Apple's). When you want to apply the 'shine' to the preview icon, just overlay this custom UIView on top of its UIImageView (or whatever you're hosting it in).
The rectangular clipping could be a little trickier. If you have a solid black background, you could overlay a frame UIImageView that has a black area with a rounded rectangular transparent region in its center. You can also do this in a more general-purpose manner via Core Graphics by drawing your image and the gloss into a view, then using CGContextClip with a rounded rectangle to carve out the rounded interior.