iOS开发——简单拍照功能实现
本帖最后由 元始天尊 于 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 = ;
UIAlertController* alert = [UIAlertController alertControllerWithTitle:Title message:Message
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelact = ;
;
alert.popoverPresentationController.sourceView = window;
alert.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
;
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
;
CGRect winrt = self.view.frame;
;
}
- (IBAction)takepic: (id)sender {
;
}
- (void)imagePickerControllerDidCancel: (UIImagePickerController *)picker {
;
}
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
;
UIImage* edited = info;
self.pic.image = edited;
self.pic.layer.cornerRadius = 9;
self.pic.layer.masksToBounds = YES;
self.pic.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)openPhotoPicker {
PHAuthorizationStatus ph_status = ;
if (ph_status == PHAuthorizationStatusNotDetermined || ph_status == PHAuthorizationStatusDenied) {
// Privacy - Photo Library Usage Description
Alert(@"提示", @"未开启相册权限");
;
return;
}
AVAuthorizationStatus av_status = ;
if (av_status == AVAuthorizationStatusNotDetermined || av_status == AVAuthorizationStatusDenied) {
// Privacy - Camera Usage Description
;
Alert(@"提示", @"未开启相机权限1");
return;
}
if (!) {
Alert(@"提示", @"没有相机");
return;
}
if (!) {
Alert(@"提示", @"没有后置摄像头");
return;
}
if (!) {
Alert(@"提示", @"没有前置摄像头");
return;
}
UIImagePickerController* imgpicker = ;
imgpicker.delegate = self;
imgpicker.allowsEditing = YES;
imgpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imgpicker.modalPresentationStyle = UIModalPresentationFullScreen;
;
}
@end
```
```
// Info.plist
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
```
页:
[1]