Search code examples
iosobjective-ciphoneswiftphotosframework

iOS Photos Framework: how to revert image asset to original


All the online document and samples showed how to edit/change assets with PHContentEditingInput and PHContentEditingOutput. I didn't find anything about resetting or reverting an image to its original. Anything wrote to renderedContentURL is considered an edit, so that's not what I want. Just share my findings here:


Solution

  • Use revertAssetContentToOriginal

    Swift:

        PHPhotoLibrary.shared().performChanges({
            let request = PHAssetChangeRequest(for:asset)
            request.revertAssetContentToOriginal()
        }, completionHandler: { success, error in
            if !success { print("can't revert asset: \(error)") }
        })
    

    Objective C:

         [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
             PHAssetChangeRequest *change = [PHAssetChangeRequest changeRequestForAsset:asset];
             [change revertAssetContentToOriginal];
         } completionHandler:^(BOOL success, NSError *error) {
             NSLog(@"Finished adding asset. %@", (success ? @"Success" : error));
         }];