Core Graphics: Mask Image, add Overlay and Underlay - ios

How can I mask an image, add an overlay and an underlay to the masked image with Core Graphics? (for instance: a document icon consists out of a mask png, a page curl png and a base with shadow) Can someone give me a best practice?

Draw the underlay with CGContextDrawImage().
Push a graphics context state with CGContextSaveGState().
Load the mask and add it to the context using CGContextClipToMask().
Draw your content.
Pop the graphics context with CGContextRestoreGState().
Draw your overlay image with CGContextDrawImage().

Related

Flipped image for Layer

so I am trying to create a flipped image for CAShapeLayer.
and I have the following code
shiplayer.contents = #imageLiteral(resourceName: "spaceship").withHorizontallyFlippedOrientation().cgImage
view.layer.addSublayer(shiplayer)
But the image rendered by the shiplayer is still the original unflipped image .
I tested it on an UIImageView, and the image is flipped properly.
What can I do to flip the image for a CALayer>?
Thanks
The CGImage is the underlying bitmap; it knows nothing of the image properties. So draw the image, flipped, into a new image graphics context and use the resulting image as the basis for a new CGImage.

SWIFT - CALayer between PNG and background image

I have seen many stackoverflow posts or tutorials on how make a mask with an image and a rectangle or a circle, but it is possible to make a mask between a png file with transparency and a background image.
In the photoshop screenshot I use the wolf png image as a mask for the background.
Any approach for this with CALayer or other in SWIFT ?
Thank you very much.
UIView as of iOS 8 has a mask property which you can set to any view with an alpha channel, including a UIImageView that has your image in it.
This is just a wrapper arround CALayer's mask property which works similarly. Set your image to the content property of your masking layer and set the masking layer as the mask for your background image layer.

how to add CAShapeLayer having transparent png image using clip path?

I have transparent png image of star shape. How can i add this transparent image into CAShapeLayer using clip path and add this path for change in ShapeLayer.
Thank you.

CSS Masking in iOS

I have an image in my iOS app. I want to use css masking technique to achieve the same image but with colors. The 1st image is the original image and the 2nd image is the output image that is required. How can i achieve it in iOS :
The drawing system on iOS is called Quartz 2D. Here's the documentation:
https://developer.apple.com/library/ios/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_overview/dq_overview.html
You will be able to use the shape of your image as a clipping path and then draw a gradient.
Alternatively, a simpler way is to draw a simple gradient with a CAGradientLayer and then mask the layer; see the documentation on CALayer and CAGradientLayer.

Apply a mask to a UIView to simulate a glass shimmer

My base image is a complex shape with multiple colors and an alpha background.
My shimmer effect is a white slanted gradient with an alpha background.
My goal is to animate the shimmer image over top of the base image, but make the alpha values of of the shimmer match those of the base image.
I've only used layer masks to hide areas based on the color, not to both show AND hide parts of an image. Will I need to create multiple masks to achieve this? A white version of my base image that will show through the masked shimmer image, all on top of my colored base image?
Since your shimmer effect is created by moving a single image/layer over a base image, you only need one mask in the shape of the non-alpha values in your base image. You will likely need to use a container view for your shimmer image view/layer that you overlay onto of the base image, then set the image mask on the container.

Resources