iOS--相机、相册
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)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"测试";
self.view.backgroundColor = [UIColor whiteColor];UIButton* but = [UIButton buttonWithType:UIButtonTypeContactAdd];
[self.view addSubview:but];
but.frame = CGRectMake(80, 280, 80, 80);
[but addTarget:self action:@selector(tap1) forControlEvents:UIControlEventTouchUpInside];
}
//相机--拍照
-(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];
}
-
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{
NSLog(@"%@",info);
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);NSLog(@"%@",image);
[picker dismissViewControllerAnimated:YES completion:nil];
}
//取消操作 -
(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
//必要实现的协议方法, 不然会崩溃
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
}
/*
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