Search code examples
objective-ccocoacore-animationcalayercgimage

New CALayer.contents does not display until view is resized


I have a CALayer that needs to display frames of video. Every time a frame is rendered (as a CGImageRef), this method is called:

- (void)displayFrame:(CGImageRef)frame {
    [view layer].contents = (id)frame;
}

What's strange is that this, at first, will not display anything. Every time view is resized, a new frame is displayed, but sticks until view is resized again.

How do I fix this?


Solution

  • Is displayFrame: being called on the main thread? If not then I have found that it will not always draw. You can try forcing it to update by using [CATransaction flush] like:

    - (void)displayFrame:(CGImageRef)frame {
        [view layer].contents = (id)frame;
        [CATransaction flush];
    }
    

    If that does not work then you might try including the layer construction code to help identify any peculiarities with this layer.