motivation1
motivation1

Reputation: 123

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();

Upvotes: 1

Views: 96

Answers (2)

Daniel Storm
Daniel Storm

Reputation: 18878

Just hide the UIButton before you take the screenshot and unhide it after you take the screenshot.

func screenshot() {
    // Hide button
    myButton.alpha = 0.0

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

    // Unhide button
    myButton.alpha = 1.0
}

To share your image to Facebook you can use a SLComposeViewController.

On a side note, you don't need to use ; at the end of each line in Swift.

Upvotes: 1

Swinny89
Swinny89

Reputation: 7373

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();

Upvotes: 1

Related Questions