码迷,mamicode.com
首页 > 其他好文 > 详细

关于选择照片和拍照

时间:2015-10-15 18:53:58      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:照片   拍照   uiimagepickercontroller   

//选择照片

- (IBAction)selectedPhoto:(id)sender {

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

    pickVC.delegate = self;

    

    [self presentViewController:pickVC animated:YES completion:nil];

    

}


//拍照

- (IBAction)cameraPhoto:(id)sender {

    //判断当前设备是否有摄像头

    BOOL isCamer = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

    if (!isCamer) {

        

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        [alertView show];

        return;

        

    }

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    //指定资源的来源:来自摄像头

    pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;

    

    pickVC.delegate = self;

    

    [self presentViewController:pickVC animated:YES completion:NULL];

    

}


//选择视频资源

- (IBAction)selectVedio:(id)sender {

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

    pickVC.delegate = self;

    

    //指定选择媒体资源类型 public.movie 视频类型  public.image 照片类型

    pickVC.mediaTypes = @[@"public.movie"];

    

    [self presentViewController:pickVC animated:YES completion:nil];


}


//拍视频

- (IBAction)camerVadio:(id)sender {

    

    //判断当前设备是否有摄像头

    BOOL isCamer = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

    if (!isCamer) {

        

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        [alertView show];

        return;

        

    }


    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;

    

    pickVC.delegate = self;

    

    //指定选择媒体资源类型 public.movie 视频类型  public.image 照片类型

    pickVC.mediaTypes = @[@"public.movie"];

    

    [self presentViewController:pickVC animated:YES completion:nil];


    

}


#pragma mark -UIImagePickerControllerDelegate

//选择一个照片、或者视频调用的协议方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    

    if ([mediaType isEqualToString:@"public.image"]) {

        //图片

        //获取图片

        UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];

        self.imgView.image = img;

        

        //判断照片的来源是否来自摄像头

        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

            //将图片保存至相册

            UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

        }

        

    }else if([mediaType isEqualToString:@"public.movie"]) {

        

        //        UIImagePickerControllerReferenceURL

        

        //1.获取到此资源的url地址

        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];

        

        //2.创建视频播放器

        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

        

        //3.弹出视频播放控制器,自动播放

        [self presentMoviePlayerViewControllerAnimated:moviePlayer];

    }


    

    //关闭模态视图

    [picker dismissViewControllerAnimated:YES completion:nil];

}


//通过UIImageWriteToSavedPhotosAlbum 保存图片成功以后,调用的方法

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

{

    NSLog(@"保存成功");

}


关于选择照片和拍照

标签:照片   拍照   uiimagepickercontroller   

原文地址:http://10577688.blog.51cto.com/10567688/1703238

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!