简体   繁体   中英

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

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)
    }
}
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
            }
    }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM