Reputation: 949
I want to store a downloaded video file into image gallery, but I am unable to store it. Please guide me how to fix it?
Thanks,
Upvotes: 1
Views: 2056
Reputation: 944
Try this one ...
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");
}
}];
});
});
Upvotes: 0
Reputation: 31083
You can save using UISaveVideoAtPathToSavedPhotosAlbum
Solution
UISaveVideoAtPathToSavedPhotosAlbum(yourVideoPath, nil, nil, nil);
To get Path of video you have to save it anywhere. you can save to destination path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString * yourVideoPath = [basePath stringByAppendingPathComponent:@"video.mov"];
[yourVideoData writeToFile:yourFilePath atomically:YES];
Upvotes: 0
Reputation: 2562
Try this this is work for me
-(void)SaveVideoTOPhotoAlbum
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *getImagePath = [basePath stringByAppendingPathComponent:videoName];
printf(" \n\n\n-Video file == %s--\n\n\n",[getImagePath UTF8String]);
UISaveVideoAtPathToSavedPhotosAlbum ( getImagePath,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
}
//
- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSLog(@"Finished saving video with error: %@", error);
//kapil here ****
}
Good luck Dear
Upvotes: 2
Reputation: 8790
can u please try this link??
this will Adds the movie at the specified path to the user’s Camera Roll album.
void UISaveVideoAtPathToSavedPhotosAlbum (
NSString *videoPath,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
further guidance you will find at above link i have specified
further usefull link is this
hope it helps ..
Upvotes: 0