找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 1247|回复: 0

iOS开发——简单拍照功能实现

[复制链接]
发表于 2021-8-20 21:11:05 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 元始天尊 于 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
回复

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-11-23 16:20 , Processed in 0.031738 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表