tags:

views:

859

answers:

1

Hi but i want dowload a video from URL and to rename the file of that video before saving it.i use UISaveVideoAtPathToSavedPhotosAlbum ?any help..what i have to do ?

 NSData *imageData = [NSData dataWithContentsOfURL:[NSURL  
 URLWithString:@"www.xyz.com/image.mp4"]];
+1  A: 

You would do something like this:

- (void) downloadVideo
   NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.xyz.com/image.mp4"]];
   NSString *tempPath = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), temp.mp4];
   [imageData writeToFile:tempPath atomically:NO];
   UISaveVideoAtPathToSavedPhotosAlbum (@ tempPath, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
);

- (void) video: (NSString *) videoPath
    didFinishSavingWithError: (NSError *) error
    contextInfo: (void *) contextInfo {
  NSLog(@"Finished saving video with error: %@", error);
}

There are two things you need to take note of:

  1. Make sure you specify the scheme of the URL. This isn't a browser, it is not going to attempt to autofill http and guess for you.
  2. If this is on the main thread it will cause a synchronous stall while dataWithContentsOfURL: happens. If the main thread stalls for more than 20 seconds the phone will assume the app has gotten stuck in an infinite loop and kill it. So if this code is on the main thread you need to either move it onto a background thread or move to an asynchronous download using NSURLConnection.
Louis Gerbarg
you have declared NSData *imageData, but you did not use it..and also you did not mention how to change file name to be saved? which is best way to dowload long video,please will you give any tutorial sir?
Mikhail Naimy
You are correct, I screwed up, you need to save it out to to a file first with `writeData:atomically:`, I have changed the code appropriately. The best way to download it is to use NSURLConnection asynchronously, as I mentioned about in the response. There are many examples of it on the web and in other SO answers.
Louis Gerbarg
error occurs like implicit declaration of function UISaveVideoAtPathToSavedPhotosAlbum'
Mikhail Naimy
i have to add any frame work?
Mikhail Naimy
You need to make sure are using the 3.0 or higher as your base SDK, but it should be in UIKit.
Louis Gerbarg