本帖最后由 元始天尊 于 2021-8-21 15:34 编辑
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(retain) IBOutlet UIImageView* pic;
@end
// ViewController.m
#import "ViewController.h"
static void Alert(NSString* Title, NSString* Message) {
UIWindow* window = UIApplication.sharedApplication.keyWindow;
UIViewController* topViewController = [window rootViewController];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:Title message:Message
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelact = [UIAlertAction actionWithTitle: @"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelact];
alert.popoverPresentationController.sourceView = window;
alert.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
[topViewController presentViewController:alert animated:NO completion:nil];
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect winrt = self.view.frame;
[self.pic setFrame:CGRectMake(0, 20, winrt.size.width, winrt.size.height - 20)];
}
- (IBAction)takepic: (id)sender {
[self openPhotoPicker];
}
- (void)imagePickerControllerDidCancel: (UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage* edited = info[UIImagePickerControllerEditedImage];
self.pic.image = edited;
self.pic.layer.cornerRadius = 9;
self.pic.layer.masksToBounds = YES;
self.pic.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)openPhotoPicker {
PHAuthorizationStatus ph_status = [PHPhotoLibrary authorizationStatus];
if (ph_status == PHAuthorizationStatusNotDetermined || ph_status == PHAuthorizationStatusDenied) {
// Privacy - Photo Library Usage Description
Alert(@"提示", @"未开启相册权限");
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {}];
return;
}
AVAuthorizationStatus av_status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (av_status == AVAuthorizationStatusNotDetermined || av_status == AVAuthorizationStatusDenied) {
// Privacy - Camera Usage Description
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {}];
Alert(@"提示", @"未开启相机权限1");
return;
}
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
Alert(@"提示", @"没有相机");
return;
}
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
Alert(@"提示", @"没有后置摄像头");
return;
}
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
Alert(@"提示", @"没有前置摄像头");
return;
}
UIImagePickerController* imgpicker = [UIImagePickerController new];
imgpicker.delegate = self;
imgpicker.allowsEditing = YES;
imgpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imgpicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imgpicker animated:YES completion:nil];
}
@end
// Info.plist
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
|