1

I have a portrait image with depth data with it and after some processing, I want to save a copy of it to a user photo album with depth data preserved (UIImage not an option in this case). For this task, I am using the function writeJPEGRepresentation() which seems to successfully save the modified image with the depth info to somewhere; however, it does not show up on the photo album.

In order it to appear on the photo album, I when try performChanges() function of PHPhotoLibrary, this time it appeared on the album, but not the modified but the original one!? Any help highly appreciated. Thanks.

Here is the code:

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")
    }
}
eral
  • 123
  • 1
  • 16

1 Answers1

0

I think the problem is that JPEG can't store depth data (as far as I know). HEIF would be the format you should use for that. Maybe you can try something like this:

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

A few remarks:

  • I assume depthdata is actually a meaningful value?
  • You can create and later pass the image data directly to the creationRequest. Then you don't need to save a file to some intermediate location (which you would need to delete afterward).
Frank Rupprecht
  • 9,191
  • 31
  • 56
  • Neither heifRepresentation() nor jpegRepresentation() or their url versions works. I think the problem may be apple did not implement them with avDepthData option selected. Without the options, image is saved but no depth data. – eral Dec 20 '19 at 15:46
  • By the way, JPEG stores depth data. I think apple uses depth enabled version. Because images in gallery has jpeg extension(and with depth) – eral Dec 23 '19 at 13:39