关于选择照片和拍照

发布时间:2020-07-12 22:09:30 作者:hmymy
来源:网络 阅读:327

//选择照片

- (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.p_w_picpath 照片类型

    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.p_w_picpath 照片类型

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

    

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


    

}


#pragma mark -UIImagePickerControllerDelegate

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

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

{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    

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

        //图片

        //获取图片

        UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];

        self.imgView.p_w_picpath = img;

        

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

        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

            //将图片保存至相册

            UIImageWriteToSavedPhotosAlbum(img, self, @selector(p_w_picpath: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)p_w_picpath:(UIImage *)p_w_picpath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    NSLog(@"保存成功");

}


推荐阅读:
  1. android拍照并给照片添加备注,以GridView显示
  2. iOS 拍照和相机的基本使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

照片 拍照 uiimagepickercontrol

上一篇:android应用开发:音乐播放器(2)--技术细节

下一篇:Sybase Msg 3101: Database in u

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》