Capturing full screenshot with status bar in iOS programmatically

后端 未结 7 2377
执笔经年
执笔经年 2020-12-14 03:45

I am using this code to capture a screenshot and to save it to the photo album.

-(void)TakeScreenshotAndSaveToPhotoAlbum
{
   UIWindow *window = [UIApplicati         


        
相关标签:
7条回答
  • 2020-12-14 04:33

    The status bar is actually in its own UIWindow, in your code you are only rendering the view of your viewcontroller which does not include this.

    The "official" screenshot method was here but now seems to have been removed by Apple, probably due to it being obsolete.

    Under iOS 7 there is now a new method on UIScreen for getting a view holding the contents of the entire screen:

    - (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
    

    This will give you a view which you can then manipulate on screen for various visual effects.

    If you want to draw the view hierarchy into a context, you need to iterate through the windows of the application ([[UIApplication sharedApplication] windows]) and call this method on each one:

    - (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates
    

    You may be able to combine the two above approaches and take the snapshot view, then use the above method on the snapshot to draw it.

    0 讨论(0)
  • 2020-12-14 04:35

    Capture the full screen of iPhone, get the status bar by using KVC:

    if let snapView = window.snapshotView(afterScreenUpdates: false) {
        if let statusBarSnapView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)?.snapshotView(afterScreenUpdates: false) {
            snapView.addSubview(statusBarSnapView)
        }
        UIGraphicsBeginImageContextWithOptions(snapView.bounds.size, true, 0)
        snapView.drawHierarchy(in: snapView.bounds, afterScreenUpdates: true)
        let snapImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    
    0 讨论(0)
  • 2020-12-14 04:37

    The following works for me, capturing the status bar fine (iOS 9, Swift)

    let screen = UIScreen.mainScreen()
    let snapshotView = screen.snapshotViewAfterScreenUpdates(true)
    UIGraphicsBeginImageContextWithOptions(snapshotView.bounds.size, true, 0)
    snapshotView.drawViewHierarchyInRect(snapshotView.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    0 讨论(0)
  • 2020-12-14 04:41

    The suggested "official" screenshot method doesn't capture status bar (it is not in the windows list of the application). As tested on iOS 5.

    I believe, this is for security reasons, but there is no mention of it in the docs.

    I suggest two options:

    • draw a stub status bar image from resources of your app (optionally update time indicator);
    • capture only your view, without status bar, or trim image afterwards (image size will differ from standard device resolution); status bar frame is known from corresponding property of application object.
    0 讨论(0)
  • 2020-12-14 04:42

    Swift, iOS 13:

    The code below (and other ways of accessing) will now crash the app with a message:

    App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.

    The window scenes and statusBarManager's really only give us access to frame - if this is still possible, I am not aware how.

    Swift, iOS10-12:

    The following works for me, and after profiling all the methods for capturing programmatic screenshots - this is the quickest, and the recommended way from Apple following iOS 10

    let screenshotSize = CGSize(width: UIScreen.main.bounds.width * 0.6, height: UIScreen.main.bounds.height * 0.6)
    let renderer = UIGraphicsImageRenderer(size: screenshotSize)
    let statusBar = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
    let screenshot = renderer.image { _ in
        UIApplication.shared.keyWindow?.drawHierarchy(in: CGRect(origin: .zero, size: screenshotSize), afterScreenUpdates: true)
        statusBar?.drawHierarchy(in: CGRect(origin: .zero, size: screenshotSize), afterScreenUpdates: true)
    }
    

    You don't have to scale your screenshot size down (you can use UIScreen.main.bounds directly if you want)

    0 讨论(0)
  • 2020-12-14 04:45

    Here is my code to take a screenshot and store it as NSData (inside an IBAction). With the sotred NSData then you can share or email or whatever want to do

    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
            if (NULL != UIGraphicsBeginImageContextWithOptions)
                UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
            else
                UIGraphicsBeginImageContext(imageSize);
    
            CGContextRef context = UIGraphicsGetCurrentContext();
    
            // Iterate over every window from back to front
            for (UIWindow *window in [[UIApplication sharedApplication] windows])
            {
                if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
                {
                    // -renderInContext: renders in the coordinate space of the layer,
                    // so we must first apply the layer's geometry to the graphics context
                    CGContextSaveGState(context);
                    // Center the context around the window's anchor point
                    CGContextTranslateCTM(context, [window center].x, [window center].y);
                    // Apply the window's transform about the anchor point
                    CGContextConcatCTM(context, [window transform]);
                    // Offset by the portion of the bounds left of and above the anchor point
                    CGContextTranslateCTM(context,
                                          -[window bounds].size.width * [[window layer] anchorPoint].x,
                                          -[window bounds].size.height * [[window layer] anchorPoint].y);
    
                    // Render the layer hierarchy to the current context
                    [[window layer] renderInContext:context];
    
                    // Restore the context
                    CGContextRestoreGState(context);
                }
            }
    
            // Retrieve the screenshot image
            UIImage *imageForEmail = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
    
        NSData *imageDataForEmail = UIImageJPEGRepresentation(imageForEmail, 1.0);
    
    0 讨论(0)
提交回复
热议问题