Kiran K
Kiran K

Reputation: 949

How to save downloaded video file in to image gallery in Objective C?

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

Answers (4)

DJ1
DJ1

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

Rajneesh071
Rajneesh071

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

Bug
Bug

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

BhavikKama
BhavikKama

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

Related Questions