iOS - Not able to save .MOV & .jpg as LivePhoto - Showing Error NSCocoaErrorDomain Code=-1 [Objective C]

317 Views Asked by At

I want to create live photo programatically with mixture of jpg and MOV. below is my code for implementation

These are links for files jpg File - http://18.220.72.107/hd_wallpapers/images/animation/43625_wallpaper-2.jpg

MOV File - http://18.220.72.107/hd_wallpapers/images/animation/43625_wallpaper-2.MOV

My MOV and JPG are coming from server. same code is working for Items placed in local files.

        NSData *urlData = [NSData dataWithContentsOfURL:url];

    if ( urlData )
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *movFilePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.MOV"];

        NSData* imgData = [NSData dataWithContentsOfURL:jpegURL];

        if(imgData){

        NSString  *jpegFilePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.jpg"];

                [imgData writeToFile:jpegFilePath atomically:YES];
                [urlData writeToFile:movFilePath atomically:YES];

                NSURL* jpgFileUrl = [NSURL fileURLWithPath:jpegFilePath];
                NSURL* movFileUrl = [NSURL fileURLWithPath:movFilePath];

                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

                    PHAssetCreationRequest* request = [PHAssetCreationRequest creationRequestForAsset];

                    [request addResourceWithType:PHAssetResourceTypePhoto fileURL:jpgFileUrl options:nil];
                   [request addResourceWithType:PHAssetResourceTypePairedVideo fileURL:movFileUrl options:nil];


                } completionHandler:^(BOOL success, NSError * _Nullable error) {
                    if (success) {

                    } else {

                    }
                }];
        }
    }

Whenever code execute it throws Error Domain=NSCocoaErrorDomain Code=-1

0

There are 0 best solutions below