SDL在iOS上保存截图

时间:2018-04-06 11:13:14

标签: ios swift ffmpeg sdl sdl-image

我正在尝试从SDL"窗口"中保存屏幕或框架。进入PNG文件,因此我使用SDL_image library。我的代码在

之下
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)

但是image.png没有保存。如果有人可以带领或帮助我。谢谢!

附加代码,保存的图片只是黑色

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)")
}

2 个答案:

答案 0 :(得分:2)

它不会在屏幕截图中显示视频帧。只有在播放视频时才会显示黑屏图像。

请尝试截取您应用的任何屏幕截图,看看它是否有效。

答案 1 :(得分:2)

我能够从SDL_window保存屏幕截图。首先,我创建一个PNG表示文件,如

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

然后你调用IMG_SavePNG()方法