1

I am building a simple motivational app - my pet project. Pretty simple. It prints a random motivational message when a button is pressed.

I would like to user to be able to press a button and crop the motivational message itself on the screen and save it to the camera roll.

I found a tutorial that does what I wanted, but it takes a FULL screenshot AND a PARTIAL screenshot.

I'm trying to modify the code so it takes ONLY a partial screenshot.

Here's the Xcode:

    print("SchreenShot")

    // Start full screenshot
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    var sourceImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(sourceImage,nil,nil,nil)

    //partial Screen Shot

    print("partial ss")
    UIGraphicsBeginImageContext(view.frame.size)
    sourceImage.drawAtPoint(CGPointMake(0, -100))
    var croppedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil,nil,nil)

Also, in the PARTIAL screenshot, it takes a snapshot of the "page" 100 pixels from the top down to the bottom. How can I make it take a snapshot of the contents of the page say 100 pixels from the top of page to 150 pixels from bottom of page?

Many, many, many thanks!

Paul Metas
  • 254
  • 3
  • 19
  • oh I see what you're asking... never mind. If you *only* want to take a partial screenshot to begin with, you'd have to create a new layer and somehow fill it with the area of the screen you want to render. I think the best solution is to still crop the screenshot of the layer *after* it's been taken, using CG (http://stackoverflow.com/a/34909260/2976878). – Hamish Jan 28 '16 at 17:07

1 Answers1

0

Your sample code draws the view into a graphics context (the snapshot), crops it, and saves it. I am altering it a little with some extra comments because it looks like you are new to this API

    // Declare the snapshot boundaries
    let top: CGFloat = 100
    let bottom: CGFloat = 150

    // The size of the cropped image
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

    // Start the context
    UIGraphicsBeginImageContext(size)

    // we are going to use context in a couple of places
    let context = UIGraphicsGetCurrentContext()!

    // Transform the context so that anything drawn into it is displaced "top" pixels up
    // Something drawn at coordinate (0, 0) will now be drawn at (0, -top)
    // This will result in the "top" pixels being cut off
    // The bottom pixels are cut off because the size of the of the context
    CGContextTranslateCTM(context, 0, -top)

    // Draw the view into the context (this is the snapshot)
    view.layer.renderInContext(context)
    let snapshot = UIGraphicsGetImageFromCurrentImageContext()

    // End the context (this is required to not leak resources)
    UIGraphicsEndImageContext()

    // Save to photos
    UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil)
Lou Franco
  • 87,846
  • 14
  • 132
  • 192