PHAssetChangeRequest

PHAssetChangeRequest

PHAssetChangeRequest Class of Photos A request to create, delete, change metadata for, or edit the content of a Photos asset, for use in a photo library change block.

class PHAssetChangeRequest

Overview

You use the PHAssetChangeRequest class to request changes for PHAsset objects. To make changes to assets in the Photos library, create a change request by using the appropriate class method for the change you want to perform.

  • Call one of the methods listed in Adding New Assets to create a new asset from an image or video file.
  • Call the deleteAssets(:)) method to delete existing assets.
  • Call the init(for:)) method to modify an asset’s content or metadata.

A change request for creating or modifying an asset works like a mutable version of the asset object. Use the change request’s properties to request changes to the corresponding properties of the asset itself. For example, the following code uses the isFavorite property of a change request to mark an asset as a favorite:

Swift:

func toggleFavorite(for asset: PHAsset) {        
    PHPhotoLibrary.shared().performChanges {
        // Create a change request from the asset to be modified.
        let request = PHAssetChangeRequest(for: asset)
        // Set a property of the request to change the asset itself.
        request.isFavorite = !asset.isFavorite
    } completionHandler: { success, error in
        print("Finished updating asset. " + (success ? "Success." : error!.localizedDescription))
    }  
}

Objective-C:

- (void)toggleFavoriteForAsset:(PHAsset *)asset {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        // Create a change request from the asset to be modified.
        PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
        // Set a property of the request to change the asset itself.
        request.favorite = !asset.favorite;
    } completionHandler:^(BOOL success, NSError *error) {
        NSLog(@"Finished updating asset. %@", (success ? @"Success." : error));
    }];
}

After Photos runs the change block and calls your completion handler, the asset’s state reflects the changes that you requested in the block.

If you create or use a change request object outside a photo library change block, Photos raises an Objective-C exception. For details on change blocks, see PHPhotoLibrary.

Inherits From

PHChangeRequest

Inherited By

PHAssetCreationRequest

Conforms To

CVarArg CustomDebugStringConvertible CustomStringConvertible Equatable Hashable NSObjectProtocol