在iPhone中捕获视频和图像

时间:2011-05-26 04:45:19

标签: iphone objective-c ipad ipod

我正在使用ImagePickerController进行视频捕获。我还需要将捕捉视频保存为图像。

所以我选择了AVCaptureSession来捕捉图像。我无法运行AVCaptureSession以及ImagePickerController。所以我需要将ImagePickerController作为AVCaptureSession的输入。

但我不知道该怎么做。请帮助我..并以正确的方式建议我。

4 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

我遇到了同样的问题..我在图像和视频方面工作,并使用下面的代码进行图像捕捉:

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

}

对于视频录制,我使用下面的代码:

- (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]; 
}

希望能帮助你们编写代码片段。

答案 2 :(得分:1)

    - (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];

    }
}

答案 3 :(得分:0)

与AVFoundation相关的Apple文档和示例代码有几个错误。有关实际从实时预览中捕获图像的代码,请参阅iOS 4: Take photos with live preview using AVFoundation