技术宅的结界

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

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 97|回复: 0
收起左侧

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

[复制链接]

288

主题

473

帖子

5202

积分

用户组: 真·技术宅

UID
2
精华
70
威望
157 点
宅币
3934 个
贡献
131 次
宅之契约
0 份
在线时间
677 小时
注册时间
2014-1-25
发表于 2021-8-20 21:11:05 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 元始天尊 于 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, 2021-9-18 15:33 , Processed in 0.035792 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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