- 遵守代理
<UIImagePickerControllerDelegate,UIAlertViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"图片获取方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//相机
if(![XXHelper isCapturePermissionGranted]){
NSString* info=@"没有相机权限";
NSLog(@"%@",info);
return;
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.modalPresentationStyle = UIModalPresentationFullScreen;
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
picker.mediaTypes = @[(NSString*)kUTTypeImage];
picker.allowsEditing = YES;//设置可编辑
//创建叠加层, 可以在系统相机上层+图片啊文字
UIView *overLayView=[[UIView alloc]initWithFrame:CGRectMake( 0,44 , kScreenWidth, kScreenHeight- 164 )];
overLayView.backgroundColor = kRandomColor;
overLayView.alpha =0.3;
//取景器的背景图片,该图片中间挖掉了一块变成透明,用来显示摄像头获取的图片;
UIImage *overLayImag=[UIImage imageNamed:@"zhaoxiangdingwei.png"];
UIImageView *bgImageView=[[UIImageView alloc]initWithImage:overLayImag];
bgImageView.center = overLayView.center;
[overLayView addSubview:bgImageView];
picker.cameraOverlayView=overLayView;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}else{
NSLog(@"设备不可用");
}
}];
[alertVC addAction:cameraAction];
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//相册
if(![XXHelper isAssetsLibraryPermissionGranted]){
NSString* info=@"没有相册权限";
// [self showAlert:info];
NSLog(@"%@",info);
return;
}
_labelView.text=@"";
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
if([UIImagePickerController isSourceTypeAvailable: picker.sourceType ]) {
picker.mediaTypes = @[(NSString*)kUTTypeImage];
picker.delegate = self;
picker.allowsEditing = NO;
}
[self presentViewController:picker animated:YES completion:nil];
}];
[alertVC addAction:photoAction];
alertVC.popoverPresentationController.sourceView = self.view;
alertVC.popoverPresentationController.sourceRect = sender.frame;
[self presentViewController:alertVC animated:YES completion:^{
}];
3 .代理方法
// UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage* upLoadImage = [[image fixOrientation:image] imgCompressed:image targetWidth:300];//将图片压缩成 300以上传服务器
_imgV.image = upLoadImage;
self.showImageView.image = upLoadImage;
//压缩图片大小
NSData* imgData = [upLoadImage compressedData];