iOS--相机、相册

2020-04-08  本文已影响0人  彬至睢阳

import "ViewController.h"

import "ViewController2.h"

import <CoreData/CoreData.h>

import <UserNotifications/UserNotifications.h>

import <UIKit/UIImagePickerController.h>

import <MobileCoreServices/MobileCoreServices.h>

import <Photos/Photos.h>

import <AVFoundation/AVCaptureDevice.h>

import <AVFoundation/AVMediaFormat.h>

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end

@implementation ViewController

//相机--拍照
-(void)tap1{

UIImagePickerController* imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;//一个布尔值,指示是否允许用户编辑选定的静态图像或电影。

// 设置可用的媒体类型、默认只包含kUTTypeImage

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];



UIAlertAction* camerAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
     imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
     imagePicker.mediaTypes =@[(NSString*)kUTTypeImage];
     imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;//摄像机使用的捕获模式。默认为照片
    
      imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;//前置摄像头
    imagePicker.cameraFlashMode =  UIImagePickerControllerCameraFlashModeOn;//闪光灯,默认是自动的
    if ( [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])//返回一个布尔值,该值指示给定的摄像机是否可用。
    {
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else{
        NSLog(@"不可用");
    }
}];
[alert addAction:camerAction];

UIAlertAction* LibraryAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
         imagePicker.mediaTypes =@[(NSString*)kUTTypeImage];
      if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
           [self presentViewController:imagePicker animated:YES completion:nil];
     }else{
         NSLog(@"不可用");
     }
   }];
   [alert addAction:LibraryAction];


UIAlertAction* PhotosAction = [UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           imagePicker.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            imagePicker.mediaTypes =@[(NSString*)kUTTypeImage];
         if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) //返回一个布尔值,该值指示设备是否支持使用指定的源类型选择媒体。
         {
              [self presentViewController:imagePicker animated:YES completion:nil];
         }else{
             NSLog(@"不可用");
         }
      }];
      [alert addAction:PhotosAction];


UIAlertAction* videoAction = [UIAlertAction actionWithTitle:@"视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
     imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes =@[(NSString*)kUTTypeMovie];//#import <MobileCoreServices/MobileCoreServices.h>必须导入这个头文件,否则报错
        imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;//前置摄像头
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;//视频录制和转码质量。视频质量设置记录的电影与内置摄像头,或通过显示在图像选择器转码
    imagePicker.videoMaximumDuration = 10*60;//视频录制的最大持续时间(以秒为单位)。默认为十分钟
         if ( [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
             [self presentViewController:imagePicker animated:YES completion:nil];
         }else{
             NSLog(@"不可用");
         }
         }];
         [alert addAction:videoAction];


UIWindow* vcW= [[UIApplication sharedApplication].windows objectAtIndex:0];
ViewController* vc = (ViewController*)vcW.rootViewController;
[vc presentViewController:alert animated:YES completion:nil];

}

//必要实现的协议方法, 不然会崩溃

}

/*
UIImagePickerController
视图控制器,用于管理用于拍照、录制电影和从用户的媒体库中选择项的系统接口。

UIImagePickerController类只支持竖屏模式。这个类是按原样使用的,不支持子类化。这个类的视图层次结构是私有的,不能修改,只有一个例外。您可以将自定义视图分配给cameraOverlayView属性,并使用该视图来显示附加信息或管理相机接口和代码之间的交互。

获取、相册权限支持iOS8.0以上,导入需要的头文件以及Photos框架#import <Photos/Photos.h>
PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus];
if (authorStatus == PHAuthorizationStatusNotDetermined) {
//用户尚未对该应用程序做出选择

}else if (authorStatus == PHAuthorizationStatusRestricted){
    //此应用程序未被授权访问照片数据。
    //用户不能改变这个应用程序的状态,可能是由于活动的限制
     NSLog(@"此应用程序未被授权访问照片数据。");
    return;
}else if (authorStatus == PHAuthorizationStatusDenied){
    //用户已明确拒绝此应用程序访问照片数据。
    NSLog(@"用户已明确拒绝此应用程序访问照片数据。");
    return;
}else if (authorStatus == PHAuthorizationStatusAuthorized){
    //用户已授权此应用程序访问照片数据。
    
}

相机权限

import <AVFoundation/AVCaptureDevice.h>

import <AVFoundation/AVMediaFormat.h>

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

  if (status == AVAuthorizationStatusAuthorized){
  //获取权限
      NSLog(@"授权");
  }else if (status == AVAuthorizationStatusDenied){
      NSLog(@"否认");
  }else if (status == AVAuthorizationStatusRestricted){
       NSLog(@"限制");
  }else if (status == AVAuthorizationStatusNotDetermined){
      NSLog(@"没决定");
  }

*/

@end

上一篇下一篇

猜你喜欢

热点阅读