how to down load Video and save it in ipad.? - iphone

How do I download video in iPad. I have the URL of a video and I need to download it and save it in my iPad database and used it offline. Can anyone help me to solve the problem?
I try this on code but it is not working to save the file. After this code I use Media Player to play it. But it is not downloaded so I can't play.
NSURL *url = // some-URL;
NSData *yourVedioFileData = [[NSData alloc] initWithContentsOfURL:[NSURLURLWithString:url]];
//Store the Data locally as Video File
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourFilePath = [documentsDirectory stringByAppendingPathComponent:#"yourVideoName.mov"];
[yourVedioFileData writeToFile:yourFilePath atomically:YES];

NSString *imagelist = URL_NAME;
NSData *yourVideoFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imagelist]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourFilePath = [documentsDirectory stringByAppendingPathComponent:#"YOUR_File_Name.mov"];
[yourVideoFileData writeToFile:yourFilePath atomically:YES];

Related

How to save pdf locally from image view

How to save the pdf locally
I have loaded the pdf doc into imageView. Now i need to save into locally into iPhone device.
Any one advice me how to save the pdf file from image view
#All
Thanks in advance
For saving the pdf in document directory use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:`ADD URL OF PDF DATA`]];
[thedata writeToFile:localFilePath atomically:YES];
For retrieving the pdf use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
if ( [[NSFileManager defaultManager] fileExistsAtPath:localFilePath] ) {
NSData *data = [NSData dataWithContentsOfFile:localFilePath];
}

iOS Saving Media Picker Selections

I am trying to create an app that saves the user's media selections from a Media Picker to be reused and avoid having to re-pick the items . Is there a way to do this with the Media Picker or any example code for this?
The only way to do this is to save the media selection in the app's sandbox i.e. Documents directory for re-use. Because, you can't access the media without user intervention i.e. you've to present media picker each time whenever you wanna pick something from the gallery. You can use something like this to save an image in app's doc directory:
+(BOOL)saveImageInDocumentsDirectory:(UIImage*)image WithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData writeToFile:savedImagePath atomically:NO];
}
And to retrieve the images:
+(UIImage*)getImageFromDocumentsDirectoryWithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
return [[[UIImage alloc] initWithContentsOfFile:savedImagePath] autorelease];
}
Hope it'll be helpful.

saving data in iphone

I am trying to download an image and audio file from the url.. i want to save the downloaded files in specific path is it possible..
please suggest me...
thanks...
save the files in the documents directory of your app.
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:#"myfile.mp3"];
BOOL success = [myMp3Data writeToFile:path atomically:YES];
Yes, it is possible.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath2 = [NSString stringWithFormat:#"%#/%#.jpg", documentsDirectory, fileName];
NSData* imageData = UIImageJPEGRepresentation(drawImage.image, 1.0);
[imageData writeToFile:filePath2 atomically:NO];

Saving a video in the form of an NSData object to file

I am calling –writeToFile:atomically: on an NSData object which contains a video I just shot.
The file is not being written and if I use the version that returns an erorr object, the error is nil.
My code is:
if ([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.movie"]) {
NSURL* url = [info valueForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:#"capturedVideo.MOV"];
[videoData writeToFile:fPath atomically:YES];
}
The return value is NO and when I check the existence of the written file, it is not there.
Any ideas?
Try creating file with NSFileManager before writing to it.

Read and write file in iPhone

How to make file read/write operation on iPhone?which is the path i need to specify to save the file? how can i get the current working directory in iPhone?
Thank You
Write to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:#"myFile"];
[data writeToFile:myFile atomically:YES];
Read a file:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
// do something useful
}