通过默认相机拍照

时间:2013-01-23 10:04:46

标签: iphone xcode uiimagepickercontroller

我正在使用UIImagePickerView控制器从我的应用程序中的iPhone默认相机拍照。完成这个过程需要很长时间。

-(IBAction)takePhoto:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:imgPicker animated:YES];
    }
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];    
    [self dismissModalViewControllerAnimated:YES];

    NSData *imageData = UIImagePNGRepresentation(pickedImage);
    path = [SAVEDIMAGE_DIR stringByAppendingPathComponent:@"image.png"];
    [imageData writeToFile:path atomically:YES];
    [tableview reloadData];
}

2 个答案:

答案 0 :(得分:2)

尝试以下几行:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePicker.delegate = self;

[self presentViewController:imagePicker animated:YES completion:nil];

答案 1 :(得分:1)

您可以使用默认相机捕捉图像,如下方法: -

-(IBAction)cameraLibraryButtonClick:(id)sender{


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage , nil];
                imagePicker.allowsEditing = NO;

                [self presentModalViewController:imagePicker animated:YES];

                newMedia = YES;
            }
            else
            {
                [self displaysorceError];
            }


}

-(void)displaysorceError{
    UIAlertView *alt = [[UIAlertView alloc] 
                        initWithTitle:@"Error" 
                        message:@"Camera Image Sorce Not Available" 
                        delegate:nil cancelButtonTitle:@"OK" 
                        otherButtonTitles:nil];
    [alt show];
    [alt release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media Info: %@", info);
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }


    }

    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}