how to take a screenshot of the iPhone programmatically?

前端 未结 11 1837
谎友^
谎友^ 2020-11-28 08:01

Is it possible in objective C that we can take the screen shot of screen and stored this image in UIImage.

相关标签:
11条回答
  • 2020-11-28 08:21

    Technical Q&A QA1703 Screen Capture in UIKit Applications

    http://developer.apple.com/iphone/library/qa/qa2010/qa1703.html

    0 讨论(0)
  • 2020-11-28 08:21
    UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:@"foo.png" atomically:YES];
    

    UPDATE April 2011: for retina display, change the first line into this:

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    {
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    }
    else
    {
        UIGraphicsBeginImageContext(self.window.bounds.size);
    }
    
    0 讨论(0)
  • 2020-11-28 08:26

    In modern way:

    Obj-C

    @interface UIView (Snapshot)
    - (UIImage * _Nullable)snapshot;
    @end
    
    @implementation UIView (Snapshot)
    
    - (UIImage * _Nullable)snapshot {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, UIScreen.mainScreen.scale);
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    @end
    

    Swift

    extension UIView {
        func snapshot() -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
            drawHierarchy(in: bounds, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
       }
    }
    
    0 讨论(0)
  • 2020-11-28 08:28
    - (void)SnapShot {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
        } else {
            UIGraphicsBeginImageContext(self.view.bounds.size);
        }
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        NSData *data = UIImagePNGRepresentation(image);
        [data writeToFile:@"snapshot.png" options:NSDataWritingWithoutOverwriting error:Nil];
        [data writeToFile:@"snapshot.png" atomically:YES];
    
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil);
    }
    
    0 讨论(0)
  • 2020-11-28 08:29
    • (UIImage *)screenshot { UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size, NO, 2.0f); [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES];

      UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

      return image; }

    0 讨论(0)
提交回复
热议问题