简体   繁体   中英

Capture Video as well as Images in iPhone

I am using ImagePickerController for Video Capturing. I need to save the capturing video as image also.

So I selected AVCaptureSession for capturing Images. I'm not able to run AVCaptureSession as well as ImagePickerController. So I need to give the ImagePickerController as input to the AVCaptureSession.

But I don't know how to do that. Please Help me.. and suggest me for the right way.

I was having same issue.. I did work on image and video and used below code for image capturing:

- (void) snapImage: (id) sendern
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType =  UIImagePickerControllerSourceTypeCamera;
    ipc.delegate = self;
    ipc.allowsImageEditing = YES;
    [self presentModalViewController:ipc animated:YES]; 

}

And for video recording i used below code:

- (void) recordVideo: (id) sender
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType =  UIImagePickerControllerSourceTypeCamera;
    ipc.delegate = self;
    ipc.allowsEditing = YES;
    ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
    ipc.videoMaximumDuration = 30.0f; // 30 seconds
    ipc.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
    // ipc.mediaTypes = [NSArray arrayWithObjects:@"public.movie", @"public.image", nil];
    [self presentModalViewController:ipc animated:YES]; 
}

Hope will help you both code snippets.

    - (IBAction) useCamera: (id)sender
    {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];
            imagePicker.allowsEditing = NO;
            [self presentModalViewController:imagePicker
                                    animated:YES];
            newMedia = YES;
        }
    }

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.popoverController dismissPopoverAnimated:true];

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = image;
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image, self,  
                                           @selector(image:finishedSavingWithError:contextInfo:),nil);
    }

}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
    if (error) 
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Save failed"
                              message: @"Failed to save image"
                              delegate: self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];

    }
}

The Apple docs and sample code related to AVFoundation has several mistakes. See iOS 4: Take photos with live preview using AVFoundation for code that actually captures images from a live preview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM