iOS: drawViewHierarchyInRect:afterScreenUpdates: doesn't draw a AVPlayerLayer content - ios

I tried to take a snapshot of a UIView which contains an AVPlayerLayer, however the video part was drawn black. On the other hand, when I use resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets: then the resulting view contains the video snapshot correctly. However, I cannot get an image out of it, so I'm stuck with UIView instead of UIImage.
I thought that unlike renderInContex, drawViewHierarchyInRect should capture all the "special" OpenGL and video layers also. Is this not possible?

Related

How to create a non-rectangular UIImageView

I'm trying to make a ViewController that presents info from a webpage like this:
However, I'm confused on one thing. How did they get the imageView to display an image that's cut off at the corner, i.e. not rectangular? Do you think they created that player card in Photoshop and used it as the background for the imageView image, or did they create it programmatically?
I wonder because the image is behind the picture of the bear, so I imagine if they created the background in Photoshop, how would they get the image behind the bear head? They can't have just created the card with the player's picture as part of it, then loaded the whole image because if they traded the player, because I'm sure they pull the player info and picture from the web so they can have a card for all players, even if they trade or acquire a new player mid-season, without having to update the app (and add the finished image to images.xcassets).
This can be composed from two CALayers at runtime. Put the picture on the bottom layer; the picture can come from anywhere - the web, the bundle, etc. the image source could be dynamic.
Put another CALayer on top, with the frame rendered with opaque colors, and a transparent cut-out for the picture in the middle:
There are a bunch of ways to do this. A simple and flexible way to do it is to create a CAShapeLayer that's the same size as the image view, with it's origin at 0,0, and add it as the UIImageView's layer's mask.
You'd create a filled UIBezierPath that maps out the part of the image you want to show, and install the bezier path's CGPath into the mask layer's path property.
The result would be that the image view is cropped so that only the part inside the shape is drawn.

Calling drawRect to draw on top of CALayer?

I current have a view with its CALayer as an AVCapturePreviewLayer, to output video from the iphone's camera. I would like to call drawRect and draw on top of this video output, like draw a simple line on the screen with the current camera capture in the background. The problem is that everything in drawRect appears behind the AVCapturePreviewLayer instead of in front of it. Is there a way to implement this functionality? Preferably without using multiple views?
Add a sublayer to the view's layer:
[view.layer addSublayer:...];
Then draw what you want on the sublayer.

How to use a UIView with a CCLayer cocos2d?

I am playing a short video with CCVideoPlayer in cocos2d and at the end of it I am capturing the very last frame of the video and I am showing it on the screen using a UIView because trying to draw it on a CCLayer makes the image show up with slightly different colors. I would imagine that is because the way that things are drawn in cocos2d is different than the way they are drawn on a UIView. So I need some way to keep the image on the screen Using the UIView, and I need to be able to draw sprites etc... on top of this view. So my question is, Is there a way to make a CCLayer transparent so that the UIView containing the image can still be seen and then draw on top of the image using a CCLayer?
(P.S I am using cocos2d v1.1.0-beta2b)

How do you get an UIImage from an AVCaptureVideoPreviewLayer?

I have already tried this solution CGImage (or UIImage) from a CALayer
However I do not get anything.
Like the question says, I am trying to get an UIImage from the preview layer of the camera. I know I can either capture a still image or use the outputsamplebuffer but my session quality video is set to photo so either of these 2 aproaches are slow and will give me a big image.
So what I thought could work is to get the image directly from the preview layer, since this has exactly the size I need and the operations have already been made on it. I just dont know how to get this layer to draw into my context so that I can get it as an UIImage.
Perhaps another solution would be to use OpenGL to get this layer directly as a texture?
Any help will be appreciated, thanks.
Quoting Apple from this Technical Q&A:
A: Starting from iOS 7, the UIView class provides a method
-drawViewHierarchyInRect:afterScreenUpdates:, which lets you render a snapshot of the complete view hierarchy as visible onscreen into a
bitmap context. On iOS 6 and earlier, how to capture a view's drawing
contents depends on the underlying drawing technique. This new method
-drawViewHierarchyInRect:afterScreenUpdates: enables you to capture the contents of the receiver view and its subviews to an image
regardless of the drawing techniques (for example UIKit, Quartz,
OpenGL ES, SpriteKit, AV Foundation, etc) in which the views are
rendered
In my experience regarding AVFoundation is not like that, if you use that method on view that host a preview layer you will only obtain the content of the view without the image of the preview layer. Using the -snapshotViewAfterScreenUpdates: will return a UIView that host a special layer. If you try to make an image from that view you won't see nothing.
The only solution I know are AVCaptureVideoDataOutput and AVCaptureStillImageOutput. Each one has its own limit. The first one can't work simultaneously with a AVCaptureMovieFileOutput acquisition, the latter makes the shutter noise.

Animated characters on an overlay of camera capture

I was wondering how the characters in this app are animated on screen. Is it possible to have a video with transparent background to put as the overlay of camera capture ? Is this just a set of UIImages animated together ? These characters seems more animated than simple gifs.
That is most likely an OpenGL animation you are seeing overlaid on the camera display. See one of the often cited answers of Brad Larson on how to do that - includes a linked example project (that dude rocks).
To achieve that effect, you use the input of the camera, put that on a planar object as a texture and render your stuff (highly animated characters or even naked, dancing robot women) on top of it, presto.

Resources