Jayson
Jayson

Reputation: 1687

Crop UIView Snapshot

I'm implementing a custom UINavigationViewController transition, and as part of the animation I want to snapshot and transform the destination UIViewController. However, I want to slice the snapshot and transform the parts independently.

There is a comment in UIView.h that seems to indicate that this is possible and even recommended:

Creating snapshots from existing snapshots (as a method to duplicate, crop or create a resizable variant) is supported. In cases where many snapshots are needed, creating a snapshot from a common superview and making subsequent snapshots from it can be more performant.

However, I cannot figure out how to actually do the cropping or create multiple snapshots from the common superview snapshot.

Is this possible?


Note: I did investigate using UIView.drawHierarchy(in:afterScreenUpdates:) to create an image that I can later crop, however this fails during a UINavigationController transition with the following error:

[Snapshotting] View (0x140853600, MTKView) drawing with afterScreenUpdates:YES inside CoreAnimation commit is not supported.

Upvotes: 1

Views: 460

Answers (1)

Jayson
Jayson

Reputation: 1687

Thanks to KMT over at the Apple forums, I found the solution:

UIView.resizableSnapshotView(from:afterScreenUpdates:withCapInsets:)

Passing in the desired cropping rect and UIEdgeInsets.zero for the cap insets correctly creates a cropped snapshot.

Upvotes: 1

Related Questions