如何将 SKSpriteNode 作为 PNG 图像保存到相机胶卷?

如何将 SKSpriteNode 作为 PNG 图像保存到相机胶卷?

问题描述:

我正在尝试将 SKSpriteNode 转换为具有透明度的 PNG 图像到相机胶卷.

I'm trying to convert SKSpriteNode as a PNG image with transparency to Camera Roll.

这会保存图像但不具有透明度:

This saves the image but not with transparency:

let image = UIImage(cgImage: (spriteNode.texture?.cgImage())!)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

这抱怨:

无法转换数据"类型的值?到预期的参数类型 'UIImage'

Cannot convert value of type 'Data?' to expected argument type 'UIImage'

let image = UIImage(cgImage: (createdCloudShadow.texture?.cgImage())!)
let image2 = UIImagePNGRepresentation(image)
UIImageWriteToSavedPhotosAlbum(image2, nil, nil, nil)

如何将 SKSpriteNode 保存为 PNG 到相机胶卷?

How can I save SKSpriteNode as PNG to camera Roll?

UIImagePNGRepresentation(_:) 函数返回 Data?文档.

所以你可能只需要重写如下:

So you probably just need to rewrite as follows:

let image = UIImage(cgImage: (createdCloudShadow.texture?.cgImage())!)
let imData = UIImagePNGRepresentation(image)
let image2 = UIImage(data: imData)
UIImageWriteToSavedPhotosAlbum(image2, nil, nil, nil)