Search code examples
iosswiftuigraphicscontext

Crop screenshot and share?


I have done the coding for full screenshot without navigation bar but I also don't want the "Motivate me" button.

enter image description here

Also if anyone know how to share the crop screenshot to Facebook?

Here is the code for screenshot:

UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size,false,0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Solution

  • Make sure that the motivate me button isn't inside the hierarchy of the view that you are taking the screenshot on. Move it onto a separate view and then use the same code that you already have. This will do exactly what you are after.

    Or if you want to crop the whole photo you can use the following, please replace the value in heightOfButton to suit:

    var heightOfButton: CGFloat = 100
    var size = UIScreen.mainScreen().bounds.size
    size.height -= heightOfButton
    UIGraphicsBeginImageContextWithOptions(size,false,0);
    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();