美文网首页
UIImagePickerController拍照和录像

UIImagePickerController拍照和录像

作者: 苏黎有个世 | 来源:发表于2017-04-14 14:30 被阅读103次

    打开摄像头显示中文,可在Info.plist添加Localizations(array)字段,添加Chinese (simplified)

    // 
    //  ViewController.m
    //  TestVideoAndPicture
    // 
    //  Created by albert on 2017/4/14.
    //  Copyright © 2017年 albert. All rights reserved.
    // 
    
    #import "ViewController.h"
    
    #import <AssetsLibrary/AssetsLibrary.h>
    
    #import <MobileCoreServices/MobileCoreServices.h>
    
    @interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    @property (weak, nonatomic) IBOutlet UIButton *testGestureButton;
    @property (weak, nonatomic) IBOutlet UIImageView *testImageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    // 选择相册中视频和照片
    - (IBAction)videos:(id)sender {
        // 检查相册是否可用
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            
            NSLog(@"sorry, no camera or camera is unavailable.");
            
            return;
        }
        
        // 创建图像选取控制器
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        // 设置图像选取控制器的来源模式为PhotoLibrary
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        // 设置图像选取控制器的类型为视频和照片
        imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, (NSString*)kUTTypeImage, nil];
        
        // 允许用户进行编辑
        imagePickerController.allowsEditing = YES;
        
        // 设置委托对象
        imagePickerController.delegate = self;
        
        // 以模视图控制器的形式显示
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
    
    // 拍照
    - (IBAction)takePictureButtonClick:(id)sender{
        
        // 检查相机模式是否可用
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            NSLog(@"sorry, no camera or camera is unavailable.");
            
            return;
        }
        
        // 获得相机模式下支持的媒体类型
        NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        
        BOOL canTakePicture = NO;
        
        for (NSString *mediaType in availableMediaTypes) {
            
            if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
                
                // 支持拍照
                canTakePicture = YES;
                
                break;
            }
        }
        
        // 检查是否支持拍照
        if (!canTakePicture) {
            
            NSLog(@"sorry, taking picture is not supported.");
            
            return;
        }
        
        // 创建图像选取控制器
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        // 设置图像选取控制器的来源模式为相机模式
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        // 设置图像选取控制器的类型为静态图像
        imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
        
        // 允许用户进行编辑
        imagePickerController.allowsEditing = YES;
        
        // 设置委托对象
        imagePickerController.delegate = self;
        
        // 以模视图控制器的形式显示
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
    
    // 录像
    - (IBAction)captureVideoButtonClick:(id)sender{
        
        // 检查相机模式是否可用
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            NSLog(@"sorry, no camera or camera is unavailable!!!");
            
            return;
        }
        
        // 获得相机模式下支持的媒体类型
        NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        
        BOOL canTakeVideo = NO;
        
        for (NSString *mediaType in availableMediaTypes) {
            
            if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
                
                // 支持摄像
                canTakeVideo = YES;
                
                break;
            }
        }
        
        // 检查是否支持摄像
        if (!canTakeVideo) {
            
            NSLog(@"sorry, capturing video is not supported!!!");
            
            return;
        }
        
        // 创建图像选取控制器
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        // 设置图像选取控制器的来源模式为相机模式
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        // 设置图像选取控制器的类型为动态图像
        imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
        
        // 设置摄像图像品质
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
        
        // 设置最长摄像时间
        imagePickerController.videoMaximumDuration = 30;
        
        // 允许用户进行编辑
        imagePickerController.allowsEditing = YES;
        
        // 设置委托对象
        imagePickerController.delegate = self;
        
        // 以模式视图控制器的形式显示
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
    
    - (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
        
        if (!error) {
            
            NSLog(@"picture saved with no error.");
            
        }else{
            
            NSLog(@"error occured while saving the picture%@", error);
            
        }
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        
        // 打印出字典中的内容
        NSLog(@"get the media info: %@", info);
        
        // 获取媒体类型
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        
        // 判断是静态图像还是视频
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { // 图片
            
            // 获取用户编辑之后的图像
            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            
            // 将该图像保存到媒体库中
            UIImageWriteToSavedPhotosAlbum(editedImage, self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
        }else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { // 视频
            
            // 获取视频文件的url
            NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
            
            // 创建ALAssetsLibrary对象并将视频保存到媒体库
            ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
            
            [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {
                if (!error) {
                    
                    NSLog(@"captured video saved with no error.");
                    
                }else{
                    
                    NSLog(@"error occured while saving the video:%@", error);
                    
                }
            }];
        }
        
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
        
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    

    参考:http://blog.csdn.net/pucker

    相关文章

      网友评论

          本文标题:UIImagePickerController拍照和录像

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