1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dorian A
  • 11
  • 3
  • Possible duplicate of [Screenshot in swift iOS?](https://stackoverflow.com/questions/25444609/screenshot-in-swift-ios) – Cœur Nov 21 '18 at 10:09
  • [`UIGraphicsGetImageFromCurrentImageContext`](https://developer.apple.com/documentation/uikit/1623924-uigraphicsgetimagefromcurrentima) will only capture UIKit context, not Cocoa2D/SpriteBuilder context! – Cœur Nov 21 '18 at 10:29
  • According to doc, you're supposed to use [CCRenderTexture](https://github.com/cocos2d/cocos2d-objc/blob/develop/cocos2d/CCRenderTexture.h) to take get an image from your view. – Cœur Nov 21 '18 at 10:42
  • I does have a Photo Library Description and Add in my Info.plist. it's the saved photos which is blank – Dorian A Nov 21 '18 at 10:42
  • SpriteBuilder is based on Cocos2D, itself based on OpenGL. So you should refer to those questions: [capturing screenshot using opengl returns black screen](https://stackoverflow.com/questions/20833559/ios7-capturing-screenshot-using-opengl-returns-black-screen), [Why is glReadPixels() failing to get GL image](https://stackoverflow.com/questions/12528365/why-is-glreadpixels-failing-in-this-code-in-ios-6-0), [How to get UIImage from EAGLView?](https://stackoverflow.com/questions/1352864/how-to-get-uiimage-from-eaglview) – Cœur Nov 21 '18 at 11:01
  • Yeah I know .. I dunno why my internship supervisor want to use this.. I will try to find a way to do it. Thanks for your help anyway. have a nice day – Dorian A Nov 21 '18 at 11:10

2 Answers2

-1

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)
}
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
  • `UIGraphicsBeginImageContextWithOptions` is deprecated starting iOS10. See the linked question https://stackoverflow.com/questions/25444609/screenshot-in-swift-ios – Cœur Nov 21 '18 at 10:36
  • UIKit code will never work to screenshot a Cocos2D-SpriteBuilder/OpenGL context. – Cœur Nov 21 '18 at 11:15
-1
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


    }
Amrita
  • 11
  • 1
  • 1
    Please add explanations with your code (and avoid deleting your posts: it's best to [edit] them instead.) – Cœur Nov 21 '18 at 09:37
  • UIKit code will never work to screenshot a Cocos2D-SpriteBuilder/OpenGL context. – Cœur Nov 21 '18 at 11:15