您可以像下面的方法从默认相机捕获图像:-
-(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];
}
}