IOS-系统相机

可以调用系统相机进行拍照和摄像,拍照后的图片可编辑,并保存到系统相册中。

#import "ViewController.h"
#import "ScanViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *Image;

@end

@implementation ViewController
{
    UIButton *scanBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建按钮
    [self creatControl];
}

- (void)creatControl {
    //相机按钮
    scanBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    scanBtn.frame = CGRectMake(100, 100, 100, 44);
    [scanBtn setTitle:@"打开相机" forState:UIControlStateNormal];
    [scanBtn addTarget:self action:@selector(scanBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scanBtn];
    
    //显示的拍照后的图片
    self.Image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 150, 200, 300)];
    [self.view addSubview:self.Image];
}

-(void)scanBtnOnClick {
  
    //打开之前一定要判断设备是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        
        UIImagePickerController *photos = [[UIImagePickerController alloc]init];
        
        photos.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        //开启以后可以摄像
        photos.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        
        //打开相机以后,设置为摄像(不设置默认为拍照)
        photos.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        
        //视频质量
        photos.videoQuality = UIImagePickerControllerQualityTypeHigh;
        
        //照片可编辑
        photos.allowsEditing =YES;
        
        photos.delegate = self;
        
        
        //显示相机
        [self presentViewController:photos animated:YES completion:nil];
        
    }
    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                       message:@"当前设备不支持访问相册"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认"
                                                             style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * _Nonnull action) {
                                                           }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                             }];
        [alert addAction:sureAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}

#pragma mark - UIImagePickerControllerDelegate代理

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    /**
     info字典里的可用信息
    UIImagePickerControllerMediaType     媒体类型(kUTTypeImage、kUTTypeMovie等)
    UIImagePickerControllerOriginalImage 原始图片
    UIImagePickerControllerEditedImage   编辑后图片
    UIImagePickerControllerCropRect      裁剪尺寸
    UIImagePickerControllerMediaMetadata 拍照的元数据
    UIImagePickerControllerMediaURL      媒体的URL
    UIImagePickerControllerReferenceURL  引用相册的URL
    UIImagePickerControllerLivePhoto     PHLivePhoto
    **/
    
    //获取拍照的原始图片
    UIImage *img = info[UIImagePickerControllerOriginalImage];
    
    //获取拍照编辑后的图片
    UIImage *img1 = info[UIImagePickerControllerEditedImage];

    //第一种方法:把照片写入相册(方便,只需要保存到相册,诶有后续操作,推荐)
    UIImageWriteToSavedPhotosAlbum(img1, nil, nil, nil);
    
    //第二种方法:把照片写入相册,需要添加Photos.framework(若后续有复杂操作,推荐)
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:img];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
    }];
    
    //显示或可以做其他任何操作
    self.Image.image = img1;
   
    //一定要回收图像选取控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 取消操作
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // 回收图像选取控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end

猜你喜欢

转载自blog.csdn.net/qq_36557133/article/details/81736817