Screenshot Only Part of Screen - Swift

前端 未结 4 1086
时光说笑
时光说笑 2021-02-07 03:43

I\'m using this Swift code to take a screenshot of my app:

UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawVi         


        
4条回答
  •  盖世英雄少女心
    2021-02-07 04:09

    For example if you want to take the screen shot of the UIView rectangle at position (50,50) with height of (100,150).

    First set the Image Context to the size of the rectangle. This sets the image size.

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,150), false, 0);
    

    Now draw the view in this context in such a way that, the visible part of the screenshot starts at (50,50)

    self.view.drawViewHierarchyInRect(CGRectMake(-50,-50,view.bounds.size.width,view.bounds.size.height) afterScreenUpdates: true)
    

    This clips the screen shot at (100,150) because we set the size of our context. Now take the UIImage from this.

    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    

提交回复
热议问题