Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On iOS10 snapshotView takes snapshot before the view is completely rendered

I have fully transparent a table and navigation controllers. Because of this, when pushing new view controllers the animation was broken. Therefore I added my own custom push transition which takes snapshot of the next view controller and animates that.

This no longer works on iOS10. snapshotView(afterScreenUpdates: true) returns a view that is pure white. I also tried getting the snapshot with the old method through graphics context and it didn't work too.

How can I be sure that the view being pushed to the navigation controller is loaded before snapshotView? Or is there a better way to solve this? This is a breaking change for me unfortunately..

like image 392
frankish Avatar asked Sep 18 '16 10:09

frankish


2 Answers

I want to just say that this is strictly an iPhone 7 simulator and iPhone 7+ simulator issue. The issue will not appear on a real device. If you need to fix this for the simulator here is a work around I found in Pod: https://github.com/NewAmsterdamLabs/ZOZolaZoomTransition/

- (UIImage *)zo_snapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapshot;
}
like image 87
HannahCarney Avatar answered Nov 03 '22 15:11

HannahCarney


This extension should fix the issue

extension UIView {
    /// Replacement of `snapshotView` on iOS 10. Fixes the issue of `snapshotView` returning a blank white screen.
    func snapshotImageView() -> UIImageView? {
        UIGraphicsBeginImageContext(bounds.size)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        layer.render(in: context)

        let viewImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return UIImageView(image: viewImage, highlightedImage: viewImage)
    }
}
like image 2
Bebekucing Avatar answered Nov 03 '22 15:11

Bebekucing