Dorian A
Dorian A

Reputation: 21

ScreenShot taken with UIGraphicsGetImageFromCurrentImageContext is blank when using Cocos2D/SpriteBuilder

I've got trouble taking a screenshot of my application using a button and the following code:

func takeAScreenshot() {
    var screenshotImage :UIImage?
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
    let context = UIGraphicsGetCurrentContext()
    layer.render(in:context!)
    screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if let image = screenshotImage, true {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
}

Every screenshot are blank. I've tried several code sample I've found on others stackOverflow Questions, but every time it's a blank or a black screenshot.

Upvotes: 1

Views: 167

Answers (2)

Manish Mahajan
Manish Mahajan

Reputation: 2092

Use these function

import UIKit
import AudioToolbox

/// Takes the screenshot of the screen and returns the corresponding image
///
/// - Parameter shouldSave: Boolean flag asking if the image needs to be saved to user's photo library. Default set to 'true'
/// - Returns: (Optional)image captured as a screenshot
func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? {
    var screenshotImage :UIImage?
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
    guard let context = UIGraphicsGetCurrentContext() else {return nil}
    layer.render(in:context)
    screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if let image = screenshotImage, shouldSave {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
    }
    return screenshotImage
}

//Take Screenshot
func takeScreenShot(viewController: UIViewController) {
     let image = self.takeScreenshot(true)
     var imagesToShare = [AnyObject]()
     imagesToShare.append(image!)

     let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil)
     activityViewController.popoverPresentationController?.sourceView = viewController.view
     viewController.present(activityViewController, animated: true, completion: nil)
}

Upvotes: -1

Amrita
Amrita

Reputation: 11

open func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? {
        var screenshotImage :UIImage?
        let layer = UIApplication.shared.keyWindow!.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
        guard let context = UIGraphicsGetCurrentContext() else {return nil}
        layer.render(in:context)
        screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        if let image = screenshotImage, shouldSave {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        }

        return screenshotImage


    }

Upvotes: -1

Related Questions