美文网首页图片处理
iOS好用的截图代码

iOS好用的截图代码

作者: Realank | 来源:发表于2019-03-22 15:52 被阅读23次
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(userDidTakeScreenshot:)
                                                     name:UIApplicationUserDidTakeScreenshotNotification
                                                   object:nil];
    
    - (void)userDidTakeScreenshot:(NSNotification *)notification {
        // 手动截取当前屏幕图片
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UIImageView* imageView = [[UIImageView alloc] initWithImage:[self takeScreenshot]];
        imageView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 20);
        [window addSubview:imageView];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [imageView removeFromSuperview];
        });
        
        
    }
    
    -(UIImage *)takeScreenshot {
        CGSize imageSize = CGSizeZero;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        
        if (UIInterfaceOrientationIsPortrait(orientation)) {
            imageSize = screenSize;
        } else {
            imageSize = CGSizeMake(screenSize.height, screenSize.width);
        }
        
        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (context) {
            for (UIWindow *window in [UIApplication sharedApplication].windows) {
                CGContextSaveGState(context);
                CGContextTranslateCTM(context, window.center.x, window.center.y);
                CGContextConcatCTM(context, window.transform);
                CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
                
                if (orientation == UIInterfaceOrientationLandscapeLeft) {
                    CGContextRotateCTM(context, M_PI_4);
                    CGContextTranslateCTM(context, 0, -imageSize.width);
                } else if (orientation == UIInterfaceOrientationLandscapeRight) {
                    CGContextRotateCTM(context, - M_PI_2);
                    CGContextTranslateCTM(context, -imageSize.height, 0);
                } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
                    CGContextRotateCTM(context, M_PI);
                    CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
                }
                
                if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
                    [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
                } else {
                    [window.layer renderInContext:context];
                }
                
                CGContextRestoreGState(context);
            }
        }
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
    

    但是这个截屏没有状态栏,想要状态栏使用这个代码:
    https://github.com/Realank/OTScreenshotHelper

    相关文章

      网友评论

        本文标题:iOS好用的截图代码

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