相機(jī)、相冊、錄影

#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    CGFloat _kWidth;
    CGFloat _kHeight;
    UIImagePickerController *_imagePickerController;/**< 圖片拾取控制器,課通過該類訪問相冊,拍照和錄影,要使用圖片拾取控制器,需要同時遵循 UINavigationControllerDelegate和UIImagePickerControllerDelegate 協(xié)議代理*/
}
@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initalizeDataSource];
    [self initalizeUserInterface];
    
}
- (void)initalizeDataSource{ //初始化數(shù)據(jù)源
    
    _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、設(shè)置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //4、設(shè)置媒體格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、設(shè)置是否允許編輯  如果該屬性為NO 就不會進(jìn)入到照片的編輯界面
        _imagePickerController.allowsEditing = YES;
        //6、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"設(shè)備不支持訪問相冊");
    }
}
//拍照
- (void)takingPictures{
    //1、獲取支持的媒體格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //2、判斷是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //3、設(shè)置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 數(shù)組內(nèi)的第一個元素是照相,第二個是錄像 */
        //4、設(shè)置媒體格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.firstObject];
        //5、設(shè)置相機(jī)模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        //6、設(shè)置攝像頭(前置、后置)
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        //7、設(shè)置閃光燈
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        //8、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"設(shè)備不支持訪問拍照");
    }
}
//錄影
- (void)takingVideo{
    
    //1、獲取支持的媒體格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //2、判斷是否支持的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //3、設(shè)置sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 數(shù)組內(nèi)的第一個元素是照相,第二個是錄像 */
        //4、設(shè)置媒體格式
        NSLog(@"%@",mediaTypes);
        _imagePickerController.mediaTypes = @[mediaTypes.lastObject];
        //5、設(shè)置相機(jī)模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        //6、設(shè)置攝像頭(前置、后置)
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        //7、設(shè)置閃光燈
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        //8、推送
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }else{
        NSLog(@"設(shè)備不支持訪問拍照");
    }
}
#pragma mark -- UIImagePickerControllerDelegate 協(xié)議方法
//用戶選擇取消
- (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) {
        //進(jìn)入相冊
        [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];
        //設(shè)置圖片的拉伸模式
        _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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容