直接代码敬之
@import MobileCoreServices;
@import AVFoundation;
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
get方法
- (UIImagePickerController*)imagePicker{
if (!_imagePicker) {
_imagePicker = [[UIImagePickerController alloc]init];
//_imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;//摄像头分前后
_imagePicker.allowsEditing= NO;
//_imagePicker.allowsImageEditing = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setBackgroundColor:[UIColor yellowColor]];
[button setFrame:CGRectMake(80, 580, 50, 50)];
button.layer.cornerRadius = 25.0;
[_imagePicker.cameraOverlayView addSubview:button];
_imagePicker.showsCameraControls = NO;
// _imagePicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI_2);
_imagePicker.delegate = self;
}
return _imagePicker;
}
事件响应实现跳转:注意貌似不能在viewdidload中操作
[self presentViewController:self.imagePicker animated:YES completion:nil];
#pragma - mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
NSLog(@"%s",__func__);
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [[UIImage alloc]init];
if (self.imagePicker.allowsEditing) {
image = [info objectForKey:UIImagePickerControllerEditedImage];
}else{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
_img.image = image;
[self.imagePicker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%s",__func__);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"cancel");
}