0

我使用这段代码来拍摄 UINavigationController 视图的快照:

@implementation UIView (Snapshot)
- (UIImage*)snapshot
{
    NSTimeInterval t0 = [[NSProcessInfo processInfo] systemUptime];
    UIGraphicsBeginImageContextWithOptions(self.frame.size, self.opaque, [[UIScreen mainScreen] scale]);
    if([self isKindOfClass:[UIScrollView class]]){
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(ctx, 0, -((UIScrollView*)self).contentOffset.y);
    }
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSTimeInterval t1 = [[NSProcessInfo processInfo] systemUptime];
    NSLog(@"time cost: %lf", t1 - t0);
    return snapshotImage;
}
@end

1、

JASidePanelController *sidePanelController = //...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:sidePanelController];
[navController.view snapshot]; // **cost 1 seconds**

2、

JASidePanelController *sidePanelController = //...
UINavigationController *navController = //...;
sidePanelController.centerPanel = navController;
[navController.view snapshot]; // **cost 0.1 seconds**

有什么方法可以提高性能?

我找到了原因:这是因为 JASideController 在 (void)styleContainer:(UIView *)container animate:(BOOL)animate duration:(NSTimeInterval)duration 中将 UIBezierPath 添加到 view.layer

4

0 回答 0