How to take a screenshot programmatically on iOS

前端 未结 20 2075
一生所求
一生所求 2020-11-22 01:42

I want a screenshot of the image on the screen saved into the saved photo library.

相关标签:
20条回答
  • 2020-11-22 02:21

    I'm answering this question as it's a highly viewed, and there are many answers out there plus there's Swift and Obj-C.

    Disclaimer This is not my code, nor my answers, this is only to help people that land here find a quick answer. There are links to the original answers to give credit where credit is due!! Please honor the original answers with a +1 if you use their answer!


    Using QuartzCore

    #import <QuartzCore/QuartzCore.h> 
    
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(self.window.bounds.size);
    }
    
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    if (imageData) {
        [imageData writeToFile:@"screenshot.png" atomically:YES];
    } else {
        NSLog(@"error while taking screenshot");
    }
    

    In Swift

    func captureScreen() -> UIImage
    {
    
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
    
        self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    
        UIGraphicsEndImageContext()
    
        return image
    }
    

    Note: As the nature with programming, updates may need to be done so please edit or let me know! *Also if I failed to include an answer/method worth including feel free to let me know as well!

    0 讨论(0)
  • 2020-11-22 02:22

    I think the following snippet would help if you want to take a full screen(except for status bar),just replace AppDelegate with your app delegate name if necessary.

    - (UIImage *)captureFullScreen {
    
        AppDelegate *_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            // for retina-display
            UIGraphicsBeginImageContextWithOptions(_appDelegate.window.bounds.size, NO, [UIScreen mainScreen].scale);
            [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
        } else {
            // non-retina-display
            UIGraphicsBeginImageContext(_bodyView.bounds.size);
            [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
        }
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    0 讨论(0)
  • 2020-11-22 02:23

    Swift 4:

    func makeImage(withView view: UIView) -> UIImage? {
    
        let rect = view.bounds
    
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
    
        guard let context = UIGraphicsGetCurrentContext() else {
          assertionFailure()
          return nil
        }
    
        view.layer.render(in: context)
    
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
          assertionFailure()
          return nil
        }
    
        UIGraphicsEndImageContext()
    
        return image
    }
    
    0 讨论(0)
  • 2020-11-22 02:25

    Get Screenshot From View

    -(UIImage *)getScreenshotImage {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(self.view.frame.size, FALSE, 2.0);
        } else {
            UIGraphicsBeginImageContextWithOptions(self.view.frame.size, FALSE, 1.0);
        }
    
        [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage * result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return result;
    }
    

    Save Image to Photos

    UIImageWriteToSavedPhotosAlbum(YOUR_IMAGE, nil, nil, nil);
    

    How-To

    UIImageWriteToSavedPhotosAlbum([self getScreenshotImage], nil, nil, nil);
    
    0 讨论(0)
  • 2020-11-22 02:28

    Get Screenshot From View :

    - (UIImage *)takeSnapshotView {
        CGRect rect = [myView bounds];//Here you can change your view with myView
        UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [myView.layer renderInContext:context];
        UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return capturedScreen;//capturedScreen is the image of your view
    }
    

    Hope, this is what you're looking for. Any concern get back to me. :)

    0 讨论(0)
  • 2020-11-22 02:29

    Considering a check for retina display use the following code snippet:

    #import <QuartzCore/QuartzCore.h> 
    
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(self.window.bounds.size);
    }
    
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    if (imageData) {
        [imageData writeToFile:@"screenshot.png" atomically:YES];
    } else {
        NSLog(@"error while taking screenshot");
    }
    
    0 讨论(0)
提交回复
热议问题