I can't save the depth effect into portrait photo

Hi, I've worked in a new project for weeks, which allows people to take pictures with dual cameras and blur the image with depth data. I think I finished the core codes and get better results than Apple works. The main problem is I can't save the blurred image into a portrait photo like the system's camera works.


I used this codes to merge the AVDepthData into JPEGs, and it looks fine.

CGImageDestinationAddAuxiliaryDataInfo(outputDestination, (__bridge CFStringRef)auxDataType, (__bridge CFDictionaryRef)auxData);


I use the PhotoLibrary to save the merged image, but I can't find any way to save them as Apple works.


[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

PHAssetCreationRequest *assetRequest = [PHAssetCreationRequest creationRequestForAsset];

[assetRequest addResourceWithType:PHAssetResourceTypePhoto data:imagedata options:nil];

...........


The image doesn't have this subtype "PHAssetCollectionSubtypeSmartAlbumDepthEffect", and I can't force the images join into the album of PHAssetCollectionTypeSmartAlbum.

When I browse the image I saved in Photos app, the image does have the depth data, and I can turn the depth blur on in the editing mode. But it doesn't show the tag as "Portrait mode" at the left-top corner of image.


Who would like to help me, and show me some codes?


Thank you.

Replies

Today I found the secret way to save the photos as Portrait mode. The Photos app shows the Portrait tag for the image. But I can't save the blurred image and original one at the same time. I try to save them by using the type of PHAssetResourceTypeAlternatePhoto as Apple save the JPEG+DNG. There's no error but it only saved one processed image.


Is there any Apple stuff to tell me the way to save two images together as adjusted and the original ones at the same time?

What is the way you found to have Photos recognize the photo as a Depth Effect portrait?

Hi there,


Please, can you provide a sample code of how to save photos as "Portrait mode" ?

I think the way to save both files at once is to create a PHAdjustmentData object with your blurred settings, then save the original Photo with PHContentEditingOutput. This creates the second edited photo (with an E in the filename). With Image Capture, you can see the two files.


[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{


    // Make a change request for adding an asset.
    PHAssetChangeRequest *changeRequest =
            [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:originalJPEGFileURL];

    // Make a content editing output for use with the change request.
    PHObjectPlaceholder *placeholder = changeRequest.placeholderForCreatedAsset;
    PHContentEditingOutput *contentEditingOutput =
            [[PHContentEditingOutput alloc] initWithPlaceholderForCreatedAsset:placeholder];

    // Apply content adjustments to the newly created asset.
    contentEditingOutput.adjustmentData = adjustmentData;
    [adjustedJPEGData writeToURL:contentEditingOutput.renderedContentURL atomically:YES];
    changeRequest.contentEditingOutput = contentEditingOutput;

} completionHandler:^(BOOL success, NSError *error) {
    if (!success) NSLog(@"Can't create asset: %@", error);
}];


What I still need to figure out is how to get a saved photo to show up in the PHAssetCollectionTypeSmartAlbum. I don't know how to programmatically force it to show up in there. If I save a photo, edit it in Photos, and turn on Portrait, then it shows up in the album.


I found a few keys in the depthMetaData, but setting these didn't work: I found these keys in kCGImageAuxiliaryDataInfoMetadata

... xmlns:depthBlurEffect="http://ns.apple.com/depthBlurEffect/1.0/">

<depthBlurEffect:SimulatedAperture>4.5</depthBlurEffect:SimulatedAperture>

<depthBlurEffect:RenderingParameters>...

Use the EXIF tag kCGImagePropertyExifCustomRendered and set its value to 8 🙂

Hi Pippo,

How can I set this tag?


Thanks