【问题标题】:Saving CIImage with Depth data with writeJPEGRepresentation function使用 writeJPEGRepresentation 函数保存具有深度数据的 CIImage
【发布时间】:2019-12-18 13:07:09
【问题描述】:

我有一张带有深度数据的肖像图像,经过一些处理后,我想将其副本保存到保留深度数据的用户相册中(UIImage 在这种情况下不是一个选项)。对于这个任务,我正在使用函数writeJPEGRepresentation(),它似乎成功地将带有深度信息的修改后的图像保存到某个地方;但是,它不会显示在相册中。

为了出现在相册中,我在尝试PHPhotoLibraryperformChanges()功能时, 这次出现在专辑里的不是修改版而是原版!? 任何帮助高度赞赏。谢谢。

代码如下:

func saveWithDepth(image : CIImage) {
    do {
        let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)
        let depthdata = DepthData
        let url = Url
        try Context.writeJPEGRepresentation(of: image, to: url!, colorSpace: colorSpace!,
                                            options: [CIImageRepresentationOption.avDepthData :depthdata!])

        PHPhotoLibrary.shared().performChanges({
            let options = PHAssetResourceCreationOptions()
            let creationRequest = PHAssetCreationRequest.forAsset()
            creationRequest.addResource(with: .alternatePhoto, fileURL: url!, options: options)
        }, completionHandler: { success, error in
            if !success {
                print("AVCam couldn't save the movie to your photo library: \(String(describing: error))")
            }
        })
    } catch {
        print("failed")
    }
}

【问题讨论】:

  • 你解决过这个问题吗?

标签: ios swift photo core-image phphotolibrary


【解决方案1】:

我认为问题在于 JPEG 无法存储深度数据(据我所知)。 HEIF 将是您应该使用的格式。也许你可以试试这样的:

func saveWithDepth(image: CIImage) {
    let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)
    let depthdata: DepthData
    let imageData = context.heifRepresentation(of: image, format: .BGRA8, colorSpace: colorSpace!,
                                               options: [CIImageRepresentationOption.avDepthData: depthdata!])

    PHPhotoLibrary.shared().performChanges({
        let options = PHAssetResourceCreationOptions()
        let creationRequest = PHAssetCreationRequest.forAsset()
        creationRequest.addResource(with: .photo, data: imageData, options: options)
    }, completionHandler: { success, error in
        if !success {
            print("Couldn't save the photo to your photo library: \(String(describing: error))")
        }
    })
}

几点说明:

  • 我认为depthdata 实际上是一个有意义的值?
  • 您可以创建并稍后将图像数据直接传递给creationRequest。然后,您无需将文件保存到某个中间位置(之后您需要将其删除)。

【讨论】:

  • heifRepresentation() 和 jpegRepresentation() 或它们的 url 版本都不起作用。我认为问题可能是苹果没有在选择 avDepthData 选项的情况下实现它们。没有这些选项,图像被保存,但没有深度数据。
  • 顺便说一下,JPEG 存储深度数据。我认为苹果使用启用深度的版本。因为图库中的图像具有 jpeg 扩展名(并且具有深度)
猜你喜欢
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2014-01-23
相关资源
最近更新 更多