美文网首页iOS学习iOS程序猿
bug?在iOS 10中snapshotViewAfterScr

bug?在iOS 10中snapshotViewAfterScr

作者: susnm | 来源:发表于2016-10-14 13:54 被阅读432次

今天在学习自定义场景动画的时候发现,貌似在iOS 10中snapshotviewafterscreenupdates方法失效了。

所以我用runtime的方法交换,实现了一下:

#import "UIView+SnapshotView.h"
#import <objc/message.h>

@implementation UIView (SnapshotView)

+ (void)load {
  Method snapshotViewAfterScreenUpdatesMethod =  class_getInstanceMethod(self, @selector(snapshotViewAfterScreenUpdates:));
  Method wwzSnapshotViewAfterScreenUpdatesMethod = class_getInstanceMethod(self, @selector(wwz_snapshotViewAfterScreenUpdates:));
  
  method_exchangeImplementations(snapshotViewAfterScreenUpdatesMethod, wwzSnapshotViewAfterScreenUpdatesMethod);
}

- (UIView *)wwz_snapshotViewAfterScreenUpdates:(BOOL)afterUpdates {
  
  if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10,0,0}]) { // iOS 10
    
    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    UIView *newView = [[UIView alloc] initWithFrame:self.frame];
    newView.layer.contents = (id)image.CGImage;
    
    return newView;
  }else { 
    return [self wwz_snapshotViewAfterScreenUpdates:afterUpdates];
  }
}

@end

这还是我第一次写文章,应该也不算文章吧,算是记录一下。希望对大家有点帮助

相关文章

网友评论

    本文标题:bug?在iOS 10中snapshotViewAfterScr

    本文链接:https://www.haomeiwen.com/subject/bhggyttx.html