Photos.framework: Cann't save remote video

Hi All

Just try to save remote video but all time get error: Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

Xcode 7.0.1.

iPhone 4s with iOS 9.0.2

[[[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:@"<url to file>"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    [PHPhotoLibrary requestAuthorization:^( PHAuthorizationStatus status ) {
        if ( status == PHAuthorizationStatusAuthorized ) {
            // Save the movie file to the photo library and cleanup.
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                // In iOS 9 and later, it's possible to move the file into the photo library without duplicating the file data.
                // This avoids using double the disk space during save, which can make a difference on devices with limited free disk space.
                if ( [PHAssetResourceCreationOptions class] ) {
                    PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
                    options.shouldMoveFile = YES;
                    PHAssetCreationRequest *changeRequest = [PHAssetCreationRequest creationRequestForAsset];
                    [changeRequest addResourceWithType:PHAssetResourceTypeVideo fileURL:location options:options];
                }
                else {
                    [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:location];
                }
            } completionHandler:^( BOOL success, NSError *error ) {
                if ( ! success ) {
                    NSLog( @"Could not save movie to photo library: %@", error );
                }
            }];
        }
        else {
        }
    }];
}] resume];

Could someone help me? What did I do wrong?

The code seems fine.

Are you sure the video is compatible with the iOS device. If the audio or video codec is not supported by iOS, the Photo Libary will reject it and you will get an error.

Sure, video was recorded in my app and stored on server (I use Parse as backend). Also before saving video was played using AVPlayer and the same url to file on server.

I've downloaded it, added to project and try to save. Local file was saved without any problem.

So I can't understand what is wrong with saving a remote file.

I suspect the issue is the temporary file created by downloadTaskWithURL.


From he documentation:

The location of a temporary file where the server’s response is stored. You must move this file or open it for reading before your completion handler returns. Otherwise, the file is deleted, and the data is lost.


I would try to move it to your apps storage before you try to add the file to the Photo-Library.

Photos.framework: Cann't save remote video
 
 
Q