How to save video clip to Photos gallery - ios 8 &

2019-08-09 19:51发布

问题:

I had work out to save video clip to Photos gallery. Here the code as below, but it not working. Even though the saveVideo return true, but I open Photos gallery can not get the video clip.

filepath

is the location on local Document directory

func saveVideo(filePath: String) {
  UISaveVideoAtPathToSavedPhotosAlbum (filePath, self,"video:didFinishSavingWithError:contextInfo:", nil)
}

func video(filePath: String, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered video has been saved to your gallery.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(ac, animated: true, completion: nil)
    }
}

回答1:

PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

        let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: filePath)!)!
        createAssetRequest.placeholderForCreatedAsset

        }) { (success, error) -> Void in
            if success {

                //popup alert success
            }
            else {
               //popup alert unsuccess
            }
    }


回答2:

Try this category for storing and updating PHAsset (Image/Video),

https://github.com/zakkhoyt/PHAsset-Utility

In this library you have to provide PHAsset object into block for category actions.



回答3:

Try this one :-

For Objective - C.

This code is for download video from internet and save it to photos library.

NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url"]];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{

NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];

dispatch_async(dispatch_get_main_queue(), ^{

    // Write it to cache directory
    NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
   [videoData writeToFile:videoPath atomically:YES];

    // After that use this path to save it to PhotoLibrary
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
     {
         if (error)
         {
            NSLog("Error");
         }
         else
         {
            NSLog("Success");
         }

        }];
    });
});

you can use PHPhotoLibrary in place of ALAssetsLibrary while developing app for iOS 9.0 or later.

You can use above code in Swift by converting to swift syntax.