Search code examples
ioscore-graphicscore-animationcore-image

CITransform vs CATransforms which and when to use?


I need to draw an overlay over camera, which on every frame draws an arrow with a different direction.

Now as I understand, because of processing every frame, I should be using the Core graphics drawing, but besides just drawing the arrow I need to give it a perspective effect depending on the camera orientation.

I managed to overlay a png on every frame which is transformed using a CIFilter with a CIPerspectiveTransform.

But now I am going to the next level by "drawing" the arrow and I am not sure what is better to apply. Should I draw that arrow and convert it into a CIImage and follow the known path of applying CIPerspectiveTransform or should I dive into Core Animation and if so... what is the equivalent of CIPerspectiveTransform in the Core Animations world.


Solution

  • Core Animation doesn't do perspective by default. It is best suited for 2D animation. However, it is possible. The transformation matrix for Core Animation is the CATransform3D type.

    In order to get perspective to work you have to manually set the .m34 field of the CATransform3D to a small negative value. If you do a Google search on "CATransform3D m34" you should find info on how you get 3D perspective from Core Animation layers.

    I haven't used Core Image's CIPerspectiveTransform filter before, so I can't easily compare them.

    Core Animation is fast, optimized for efficient use of the graphics hardware, and (obviously) for animation. Core Image is also hardware-accelerated, but doesn't seem as well suited for animation.

    I'd say if you're Core Image filter based code gives you acceptable performance, use that. It's a known. Core Animation is pretty complex and the documentation isn't great. It takes a lot of digging and experimentation to get the hang of it, particularly for perspective drawing.