I have a mp4 video in my application (at Document folder)! I want to copy or move it to Photolibrary... how can I do that? please help me!
Asked
Active
Viewed 1,524 times
3 Answers
1
If you don't want to use assest library, here is the code
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoFile = [documentDirectory stringByAppendingPathComponent:@"video.mp4"];
UISaveVideoAtPathToSavedPhotosAlbum(videoFile, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
- (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
if(error)
NSLog(@\"didFinishSavingWithError: %@\", error);
}

Thilina Chamath Hewagama
- 9,039
- 3
- 32
- 45
-
UISaveVideoAtPathToSavedPhotosAlbum does not work for me :(! and also no error occured... – Younes Jafari Feb 20 '13 at 20:41
-
Also `UIVideoAtPathIsCompatibleWithSavedPhotosAlbum` funciton can be uset to check if video is compatible. – jamapag Feb 20 '13 at 21:28
0
I think you can use:
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock
method from ALAssetsLibrary

jamapag
- 9,290
- 4
- 34
- 28
0
To move to the Photo Library (called the Camera Roll, in iOS):
NSString *fileURLString = @"file://....somepath.../somefile.mp4";
NSURL *fileURL = [NSURL URLWithString:fileURLString];
// Check compatibility
if ([[ALAssetsLibrary new] videoAtPathIsCompatibleWithSavedPhotosAlbum:fileURL]) {
// Save to Photo Library / Camera Roll
[[ALAssetsLibrary new] writeVideoAtPathToSavedPhotosAlbum:fileURL completionBlock:nil];
} else {
NSLog(@"Incompatible type. Cannot save to Photo Library.");
}
To copy to any folder, though you probably don't want to do this:
NSFileManager *fileManager = [NSFilemanager defaultManager];
NSURL *originalDirectory = [NSURL fileURLWithPath:@"/originalDir/filename.mp4"];
NSURL *newDirectory = [NSURL fileURLWithPath:@"/newDirectory/filename.mp4"];
[fileManager moveItemAtURL:originalDirectory toURL:newDirectory error:nil];
If you need to download first, then do something like this:
Use ALAssetsLibrary
as follows:
#import <AssetsLibrary/AssetsLibrary.h>
// ...
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:yourVideoURL completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// Do something, like show an Alert View describing the error
}
}];

JaredH
- 2,338
- 1
- 30
- 40