Initializer

init(placeholderForCreatedAsset:)

Creates an editing output for use in adding a new asset to the photo library.

Declaration

init(placeholderForCreatedAsset: PHObjectPlaceholder)

Parameters

placeholderForCreatedAsset

A placeholder object that ties the editing output to a PHAssetChangeRequest object for creating a new asset.

Return Value

An initialized content editing output.

Discussion

Use this method if you want to add a new asset to the Photos library with edited content, as opposed to editing the content of an asset after adding it to the library. For example, you might use this option if your app applies filters to photos it captures with the device camera—instead of saving only the filtered image to the Photos library, your app can save both the filtered and the original image, allowing the user to revert to the original image or apply different filters later. The code below illustrates such a workflow:

[[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);
}];

In this example, the app first uses a PHAssetChangeRequest object to request creation of a new asset with the unedited image captured from the camera (originalJPEGFileURL). Then, it create a PHContentEditingOutput object with the placeholderForCreatedAsset object provided by the change request. Finally, it provides filtered image data (adjustedJPEGData) and adjustment data describing the filters (adjustmentData) to the editing output and adds the editing output to the change request.