display.captureScreen()

类型 函数
display.*
返回值 DisplayObject(显示对象)
修订 版本 2024.3703
关键词 屏幕截图,捕获屏幕,保存屏幕
另请参阅 display.save()
display.capture()
display.captureBounds()

概述

捕获屏幕内容并将其作为新的显示对象返回。您可以选择将捕获内容保存为文件到设备的相册。

调用此方法会将捕获的图像放置在屏幕上所有其他显示对象的顶部。使用 object:removeSelf() 从屏幕上移除此对象。

注意

此捕获函数将仅捕获 OpenGL 渲染的内容。它**不会**捕获原生显示对象,例如文本输入框/字段、Web 弹出窗口、广告等。

注意事项

Android

当应用暂停时,Android 操作系统会从内存中移除所有 OpenGL 纹理。当应用恢复时,Corona 必须重新加载所有图像,但捕获的图像不再存在于内存中。如果您需要在 Android 中恢复捕获的图像,可以使用以下解决方案:

  • 通过 display.save() 函数将返回的捕获图像保存到文件。请注意,您不能在 Android 的 "applicationSuspend""applicationExit" 事件 中使用 display.save() 函数,因为内存中没有 OpenGL 纹理可供保存。
  • 使用捕获对象的边界,通过 display.newImageRect() 显示保存到文件的图像。

此外,如果您将 saveToPhotoLibrary 选项设置为 true,则必须在 build.settings 文件中设置以下权限:

settings =
{
    android =
    {
        usesPermissions =
        {
            "android.permission.WRITE_EXTERNAL_STORAGE",
        },
    },
}

iOS

在 iOS 上,如果您将 saveToPhotoLibrary 选项设置为 true,则必须在 build.settingsplist 表中包含以下键/描述。当系统提示用户允许访问时,相关的描述将作为警报的一部分显示。请注意,您可以根据自己的喜好自定义这些描述,甚至可以对其进行本地化(指南)。

settings =
{
    iphone =
    {
        plist =
        {
            NSPhotoLibraryUsageDescription = "This app would like to access the photo library.",
            NSPhotoLibraryAddUsageDescription = "This app would like to add the photo library.",
        },
    },
}

macOS

将屏幕截图图像保存为 JPEG 文件到当前用户的“图片”文件夹。

请注意,如果您计划将应用程序提交到 Mac App Store,它将被**沙盒化**。这意味着必须请求在“图片”文件夹中读取/写入文件的特殊权限。为此,只需在settingsosxbuild.settings 表中添加 entitlements 条目,如下所示。有关更多详细信息,请参阅 Apple 的文档

settings = 
{
    osx = {
        entitlements = {
            ["com.apple.security.assets.pictures.read-write"] = true,
        },
    },
}

Windows

将屏幕截图图像保存为 PNG 文件到用户的我的图片目录下的以 Corona 应用命名的子目录中。对于 Corona 模拟器,此目录将是我的图片\Corona Simulator。对于Corona 构建的桌面应用程序,此目录将是我的图片\<AppName>.

启动时捕获

如果您需要在应用程序启动时捕获显示对象,例如在执行 main.lua 初始化应用程序时,则必须在 timer.performWithDelay() 调用中调用 display.captureScreen()。建议延迟至少 100 毫秒。

local function captureWithDelay()
    local capture = display.captureScreen()
end

timer.performWithDelay( 100, captureWithDelay )

语法

display.captureScreen( [saveToPhotoLibrary] )

saveToPhotoLibrary (可选)

布尔值 如果为 true,则将图像添加到设备的相册中。对于 Android 和 iOS 设备,这也需要满足注意事项中列出的要求。

示例

-- Fill the screen with a green rectangle
local rect = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
rect:setFillColor( 0, 1, 0.2 )

-- Draw a circle on the screen
local circle = display.newCircle( 155, 100, 36 )
circle:setFillColor( 1, 1, 1, 0.6 )

local function captureOnEvent()

    -- Capture the screen
    local screenCap = display.captureScreen( true )

    -- Remove the objects from the screen
    rect:removeSelf()
    circle:removeSelf()

    -- Scale the screen capture, now on the screen, to half its size
    screenCap:scale( 0.5, 0.5 )
    screenCap.x = display.contentCenterX
    screenCap.y = display.contentCenterY
 
    -- Alert the user to look in the library (device) or on the desktop (Simulator) for the screen capture
    local alert = native.showAlert( "Success", "Screen Capture Saved to Library", { "OK" } )
end

timer.performWithDelay( 500, captureOnEvent )