14

For iOS9, ALAssetsLibrary is deprecated. So how to change it as PHPPhotoLibrary instead of ALAssets?

if (RecordedSuccessfully && recording == NO) {
    //----- RECORDED SUCESSFULLY -----
    NSLog(@"didFinishRecordingToOutputFileAtURL - success");
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)
         {
             if (error)
             {

             }
         }];
    }

// i have tried this, but didnt work

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:outputFileURL];

        NSParameterAssert(createAssetRequest);
    }
                                      completionHandler:^(BOOL success, NSError *error) {}];
    }
}
Piyush Mathur
  • 1,617
  • 16
  • 33
dicle
  • 1,122
  • 1
  • 12
  • 40

1 Answers1

22
// Save to the album
   __block PHObjectPlaceholder *placeholder;

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:outputFileURL];
        placeholder = [createAssetRequest placeholderForCreatedAsset];

    } completionHandler:^(BOOL success, NSError *error) {
        if (success)
        {
           NSLog(@"didFinishRecordingToOutputFileAtURL - success for ios9");
        }
        else
        {
            NSLog(@"%@", error);
        }
    }];
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
dicle
  • 1,122
  • 1
  • 12
  • 40
  • 1
    this is the correct answer, you should mark it as such :) – jjjjjjjj Mar 15 '16 at 17:33
  • 2
    How about assetURL? How can I get url after saving? – Besat Feb 15 '17 at 16:27
  • How is `sharedPhotoLibrary` declared? – instanceof May 18 '17 at 18:51
  • 1
    A good way to replace the assetURL from ALAssetLibrary is to use the PHObjectPlaceholder. From the example above just use placeholder.localIdentifier this will give you the path to the item in your photo library. This is from my experience playing around with it. @Besat – Derek Saunders Dec 26 '17 at 02:37