how to import video from iphone with no time duration - iphone

can any one help me please
i just want to pick video from iphone which already recorded with help of uiimagepicker controller.
i want to copy my apps document directory.
---------------------------------- prashant

I am unsure of what you mean with no time duration. But you can do the copying/moving in the UIImagePickerDelegate method.
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *movieURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *saveURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:#"movie.mov"]];
NSError *error;
if (!([[NSFileManager defaultManager] moveItemAtURL:movieURL toURL:saveURL error:&error])) {
NSLog(#"Error saving: %#", [error localizedDescription]);
}
[picker dismissModalViewControllerAnimated:YES];
}

Related

how to save capture the video in local file

I want capture the video using iPhone application. In this video file i want store the local application. how to save this file in locally. Please help me.
Thanks in Advance
You can do it :
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
[picker dismissModalViewControllerAnimated:YES];
}

How to save recorded video into photo album?

Following code is to save image took from camera into photo album.
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
image = [info objectForKey:UIImagePickerControllerEditedImage];
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
How to save Recoded video into photoAlbum?
Check Below code...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.movie"]){
// Saving the video / // Get the new unique filename
NSString *sourcePath = [[info objectForKey:#"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
- (void)saveMovieToCameraRoll
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:movieURL
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error)
[self showError:error];
else
[self removeFile:movieURL];
dispatch_async(movieWritingQueue, ^{
recordingWillBeStopped = NO;
self.recording = NO;
[self.delegate recordingDidStop];
});
}];
[library release];
}
This is the code snippet from apple example rosywriter. Sould work.
movieURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#%#", NSTemporaryDirectory(), #"Movie.MOV"]];
[movieURL retain];
The above lines to create file and path for the video.
- (void) startRecording
{
dispatch_async(movieWritingQueue, ^{
if ( recordingWillBeStarted || self.recording )
return;
recordingWillBeStarted = YES;
// recordingDidStart is called from captureOutput:didOutputSampleBuffer:fromConnection: once the asset writer is setup
[self.delegate recordingWillStart];
// Remove the file if one with the same name already exists
[self removeFile:movieURL];
// Create an asset writer
NSError *error;
assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType:(NSString *)kUTTypeQuickTimeMovie error:&error];
if (error)
[self showError:error];
});
}
This function is used to start recording video into that movieURL file using avassetwriter.
Try below code
- (void)saveVideo:(NSURL *)videoUrl {
NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
[videoData writeToFile:#"YOUR_PATH_HERE" atomically:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *type = [mediaDict objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video
NSURL *videoURL [mediaDict objectForKey:UIImagePickerControllerMediaURL];
[self saveVideo:videoUrl];
}
}
or you can try this also
Saving the video to the documents directory is as follows
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
[picker dismissModalViewControllerAnimated:YES];
}

Captured Video on iPhone, trying to move it to another directory

Using UIImagePickerController, I have captured a video. When I call [picker stopVideoCapture], then the following delegate method is called:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSLog(#"attempting to copy: %# to: %#", [url absoluteString], [NSString stringWithFormat:#"%#/rawMovie.MOV",path]);
if ([fileManager moveItemAtPath:[url absoluteString] toPath:[NSString stringWithFormat:#"%#/rawMovie.MOV",path] error:&error] != YES)
NSLog(#"Can't move file with error: %#", [error localizedDescription]);
}
When this method is called however, it returns an error reading:
The operation couldn’t be completed. (Cocoa error 4.)
As far as I have been able to tell, this means that the file cannot be copied for some unknown reason. Can anyone give me a better answer as to why this error is being thrown? Or, better yet, can anyone tell me the best way to save the captured video directly to the app's documents directory?
Thanks,
James
Why dont you put breakpoints & see the output of *path and url. Also do you have enough disk space to store your video on the device?
I think you need to use the default file manager:
NSFileManager* fileManager = [NSFileManager defaultManager];
You can also log the error's userinfo for (hopefully) more details.
NSLog(#"Can't move file with error: %#", [error userInfo]);

Save image from iPhone camera directly to documents folder and not to camera roll?

I am trying to figure out how to take a image with the iPhone camera and save it directly to the apps document folder, and if possible not in the camera roll.
I have my app showing the camera, and I can save the image to the camera roll, but I want to save it directly to the apps document folder.
Any idea ?
Thanks for the help.
You can try doing this in your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method:
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(pickedImage);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"imageName.png"];
NSError * error = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&error];
if (error != nil) {
NSLog(#"Error: %#", error);
return;
}
Let me know if that works for you.

How to move a movie from Camera Roll to app's Documents folder?

How can I copy/move a movie from Camera Roll to an app's own Documents folder?
You can implement your delegate method imagePickerController:didFinishPickingMediaWithInfo: like this –
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL * fileURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * storePath = [documentsDirectory stringByAppendingPathComponent:#"myMovie.MOV"];
NSError * error = nil;
[[NSFileManager defaultManager] copyItemAtURL:fileURL
toURL:[NSURL fileURLWithPath:storePath]
error:&error];
if ( error )
NSLog(#"%#", error);
[self dismissModalViewControllerAnimated:YES];
}
Once the user selects a movie, you get a file URL to a temporary file using the key UIImagePickerControllerMediaURL in the info dictionary passed as argument to the delegate method. You can get that and then copy the file to the documents directory using NSFileManager.