美文网首页
相机、相册、录影

相机、相册、录影

作者: 张家杰仔 | 来源:发表于2017-04-06 10:40 被阅读24次
#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    CGFloat _kWidth;
    CGFloat _kHeight;
    UIImagePickerController *_imagePickerController;/**< 图片拾取控制器,课通过该类访问相册,拍照和录影,要使用图片拾取控制器,需要同时遵循 UINavigationControllerDelegate和UIImagePickerControllerDelegate 协议代理*/
}
@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initalizeDataSource];
    [self initalizeUserInterface];
    
}
- (void)initalizeDataSource{ //初始化数据源
    
    _imagePickerController = [[UIImagePickerController alloc]init];
    _imagePickerController.delegate = self;
    
    _kWidth = [UIScreen mainScreen].bounds.size.width;
    _kHeight = [UIScreen mainScreen].bounds.size.height;
}
- (void)initalizeUserInterface{ //初始化用户界面
    [self.view addSubview:self.imageview];
    [self.view addSubview:self.button];
}
#pragma mark -- private methods
//相册
- (void)accessPhotoALbm{
//1、获取支持的媒体格式
   NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    //2、判断是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        //3、设置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //4、设置媒体格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、设置是否允许编辑  如果该属性为NO 就不会进入到照片的编辑界面
        _imagePickerController.allowsEditing = YES;
        //6、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"设备不支持访问相册");
    }
}
//拍照
- (void)takingPictures{
    //1、获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //2、判断是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //3、设置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 数组内的第一个元素是照相,第二个是录像 */
        //4、设置媒体格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、设置相机模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        //6、设置摄像头(前置、后置)
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        //7、设置闪光灯
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        //8、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"设备不支持访问拍照");
    }
}
//录影
- (void)takingVideo{
    
    //1、获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //2、判断是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //3、设置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 数组内的第一个元素是照相,第二个是录像 */
        //4、设置媒体格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.lastObject];
        //5、设置相机模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        //6、设置摄像头(前置、后置)
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        //7、设置闪光灯
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        //8、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"设备不支持访问拍照");
    }
}
#pragma mark -- UIImagePickerControllerDelegate 协议方法
//用户选择取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//用户选择某个媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@",info);
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //赋值
    self.imageview.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 点击事件
- (void)action_button:(UIButton *)sender{
//弹窗提示用户
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //进入相册
        [self accessPhotoALbm];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self takingPictures];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"录影" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self takingVideo];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark -- Getters
- (UIImageView *)imageview{
    if (!_imageview){
        _imageview = [[UIImageView alloc]initWithFrame:CGRectMake(_kWidth * 0.125 , _kHeight * 0.1, _kWidth * 0.75, _kHeight * 0.3)];
        _imageview.backgroundColor = [UIColor redColor];
        //设置图片的拉伸模式
        _imageview.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageview;
}
- (UIButton *)button{
    if (!_button){
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(_kWidth * 0.25, _kHeight * 0.5, _kWidth * 0.5, _kHeight * 0.05);
        [_button setTitle:@"选择图片" forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:20];
        
        [_button addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
@end


相关文章

网友评论

      本文标题:相机、相册、录影

      本文链接:https://www.haomeiwen.com/subject/xwytattx.html