iOS之录像 拍照

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之录像 拍照相关的知识,希望对你有一定的参考价值。

一、基本属性

UIKit -> UIImagePickerController

 cameraCaptureMode 区分拍照  还是录像

 区分 拍照 录像 cameraCaptureMode:

 录像:UIImagePickerControllerCameraCaptureModeVideo

 拍照:UIImagePickerControllerCameraCaptureModePhoto

 

   区分 选择摄像头 相册sourceType

 摄像头:UIImagePickerControllerSourceTypeCamera

 相册:UIImagePickerControllerSourceTypePhotoLibrary

 相册:UIImagePickerControllerSourceTypeSavedPhotosAlbum

 

 区分 前后摄像头 cameraDevice

 前摄像头:UIImagePickerControllerCameraDeviceFront

 后置摄像头:UIImagePickerControllerCameraDeviceRear

  设置是否显示  控制控件

 showsCameraControls 默认显示控制控件

  设置拍照

 takePicture

 

 录像

 startVideoCapture

 stopVideoCapture

 

 设置视频清晰度video

 

 设置闪光cameraFlashMode

 1、UIImagePickerControllerCameraFlashModeOff 关闭的

 2、UIImagePickerControllerCameraFlashModeAuto 自动 默认

 3、UIImagePickerControllerCameraFlashModeOn 开启

 

 UIImagePickerControllerQualityTypeHigh

 UIImagePickerControllerQualityTypeMedium

 UIImagePickerControllerQualityTypeLow

 UIImagePickerControllerQualityType640x480

 UIImagePickerControllerQualityTypeIFrame1280x720

 UIImagePickerControllerQualityTypeIFrame960x540

 

 videoMaximumDuration  设置视频最大的持续时间

 cameraOverlayView 设置调用摄像头视图页面的  覆盖视图

 设置拍照页面的形态 cameraViewTransform

 代理

 ?????需要导入两个代理

 UINavigationControllerDelegate、UIImagePickerControllerDelegate

 

 在采集完成之后去  调用  不区分是拍照还是摄像都同样的调用这个方法

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;

 

 采集取消的时候调用

 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

 

 保存图片到相册UIImageWriteToSavedPhotosAlbum:

 保存完成之后的回调

 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

 

代码示例、

导入框架:
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIView+Transfrom.h"

宏定义:
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕宽
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高

全局变量和导入代理
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UILabel *showTypeLabel;  //显示拍照或者录像的控件
    BOOL isMovie; //判断拍照  摄像的依据
    
    UIImageView *showMediaView; //显示拍照录像结果的视图
}
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor greenColor];
    
    UISwitch *mediaSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 50, 50, 50) ];
    [mediaSwitch addTarget:self action:@selector(changeMdiaType:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mediaSwitch];
    
    
    showTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(mediaSwitch.frame), CGRectGetMinY(mediaSwitch.frame)-40, 50, 30)];
    showTypeLabel.textAlignment = NSTextAlignmentCenter;
    showTypeLabel.text = @"拍照";
    showTypeLabel.textColor = [UIColor lightGrayColor];
    showTypeLabel.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:showTypeLabel];
    
 
    showMediaView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 250, 100)];
    showMediaView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:showMediaView];
    
  
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"camera" forState:UIControlStateNormal];
     button.backgroundColor = [UIColor brownColor];
     [button addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:button];
    
    
}

开关的触发方法  在这里给label添加特效   改变标签的文本
- (void)changeMdiaType:(UISwitch *)sender{
    [showTypeLabel rotation];
    showTypeLabel.text = sender.isOn != YES ? @"拍照" : @"录像";
    用来决定是录像还是拍照
    isMovie = sender.isOn;
    
}

按钮的触发事件
- (void)doit:(UIButton *)sender{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    pickerController.delegate = self;
//    选择摄像头设备
//    默认的是选择相册
    pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    if (isMovie == YES) {
        //    选择媒体类型  默认也是拍照的  拍照的时候不选择媒体类型  不会崩溃  是因为默认设置的是KUTTypeImage -> 包含在MobileCoreServices这个框架里边 需要的内容不是OC里面的字符串的类型  需要强制转换
        //    录制视频  类型要选择kUTTypeMovie 它里面包含了Audio Video
        pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
        
        //    设置录像 默认拍照 **********一定要先选择媒体的类型 否则程序崩溃
        pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    }
    

    
    [self presentViewController:pickerController animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) {
        NSLog(@"录完了");

NSString *path = (NSString *)[info[UIImagePickerControllerMediaURL] path];
        
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        





    }
    if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {
        NSLog(@"拍完了");
拍的图片
        NSLog(@"%@",info[UIImagePickerControllerOriginalImage]);
        
        UIImage *finishImage = info[UIImagePickerControllerOriginalImage];
        
        showMediaView.image = nil;
        showMediaView.image = finishImage;
        
//        把图片转成二进制 保存起来
        NSData *imageData = UIImageJPEGRepresentation(finishImage, 0.1);
        
        NSData *imageData1 =   UIImagePNGRepresentation(finishImage);
        
//        把照片保存到相册的方法
        UIImageWriteToSavedPhotosAlbum(finishImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


//视频保存到相册之后  调用
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];
    AVPlayerViewController *vc = [[AVPlayerViewController alloc] init];
    vc.player = player;
    
    [self presentViewController:vc animated:YES completion:nil];
    
    [vc.player play];
    
}



//把照片保存到相册成功  调用这个方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"保存成功");
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"取消");
    [self dismissViewControllerAnimated:YES completion:nil];
}

 

 

 

以上是关于iOS之录像 拍照的主要内容,如果未能解决你的问题,请参考以下文章

android R 无法调起拍照录像问题

Android 开发 Camera类的拍照与录像

input[type=file]标签 本地文件拍照录像 上传的兼容性问题

input[type=file]标签 本地文件拍照录像 上传的兼容性问题

自拍机器人(摄影 拍照 录像)

Android 知识要点整理----拍照和录像