Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL save screenshot on iOS

I am trying to save a screen or frame from the SDL's "window" into a PNG file and so I'm using SDL_image library. My code is below

IMG_Init(Int32(IMG_INIT_PNG.rawValue))
let screenShot = SDL_CreateRGBSurface(0, 640, 480, 32, 0, 0, 0, 0)
SDL_SetRenderTarget(renderer, texture)
SDL_RenderReadPixels(renderer, nil, Uint32(SDL_PIXELFORMAT_ARGB8888), screenShot?.pointee.pixels, (screenShot?.pointee.pitch)!)
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
IMG_SavePNG(screenShot, "\(documentsPath)/image.png")
SDL_FreeSurface(screenShot)

But the image.png was not saved. If anyone can lead or help me. Thank you!

Additional code, the image saved is just black

IMG_Init(Int32(IMG_INIT_PNG.rawValue))
let screenShot = SDL_CreateRGBSurface(Uint32(SDL_SWSURFACE), 640, 480, 32, 0, 0, 0, 0)
// SDL_SetRenderTarget(renderer, texture)
SDL_RenderReadPixels(renderer, nil, Uint32(SDL_PIXELFORMAT_ARGB8888), screenShot?.pointee.pixels, (screenShot?.pointee.pitch)!)
// Save to documents directory
let fileManager = FileManager.default
do {
    let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let fileUrl = documentDirectory.appendingPathComponent("imageLOL.png")
    if !fileManager.fileExists(atPath: fileUrl.path) {
        print("File NO exists")
        // Create file at path
        let data = Data()
        let createFile = fileManager.createFile(atPath: fileUrl.path, contents: data, attributes: nil)
        if createFile {
            print("Create file success")
        } else {
            print("Create file failed")
        }
    } else {
        print("File exists")

    }
    let result = IMG_SavePNG(screenShot, fileUrl.path)
    print("result = \(result)")
    // After saving screenshot
    let image = UIImage(contentsOfFile: fileUrl.path)
    let imageData = UIImagePNGRepresentation(image!)
    print("image length = \(String(describing: imageData?.count))")
    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
    SDL_FreeSurface(screenShot)
} catch {
    print("Error docs = \(error)")
}
like image 956
Lawrence Gimenez Avatar asked Apr 06 '18 11:04

Lawrence Gimenez


2 Answers

It will not give you the video frame in the screenshot. It will give you black screen image only when you are playing the video.

Please try to take the screenshot of any of your app's screen and see if it is working.

like image 184
Abhishek Avatar answered Oct 22 '22 17:10

Abhishek


I was able to save the screenshot from the SDL_window. First I create a PNG representation file like

let image = UIImage()
let data = UIImagePNGRepresentation(image)
try data?.write(to: fileUrl)

then after that you call IMG_SavePNG() method

like image 22
Lawrence Gimenez Avatar answered Oct 22 '22 17:10

Lawrence Gimenez