• iOS开发小技巧--相机相册的正确打开方式


    iOS相机相册的正确打开方式- UIImagePickerController

    • 通过指定sourceType来实现打开相册还是相机
    UIImagePickerControllerSourceTypePhotoLibrary, // 相册库,范围比PhotosAlbum要大
    UIImagePickerControllerSourceTypeCamera, // 相机
    UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相册
    
    • 打开之前一定判断设备是否可用[UIImagePickerController isSourceTypeAvailable:type]
    /// 打开相机
    - (void)openCamera
    {
        [self openImagePickerControllerWithType:UIImagePickerControllerSourceTypeCamera];
    }
    /// 打开相册
    - (void)openAlbum
    {
        [self openImagePickerControllerWithType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    /// 打开ImagePickerController的方法
    - (void)openImagePickerControllerWithType:(UIImagePickerControllerSourceType)type
    {
        // 设备不可用  直接返回
        if (![UIImagePickerController isSourceTypeAvailable:type]) return;
    
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        imgPicker.sourceType = type;
        imgPicker.delegate = self;
        [self presentViewController:imgPicker animated:YES completion:nil];
    }
    
    • 代理方法中,获取选中图片注意:一定记得dismiss
    #pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate
    // 选择照片之后
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
    {
    //    ChaosLog(@"%@",info);
        // 获取用户选择的图片
        UIImage *image = info[UIImagePickerControllerOriginalImage];
    
        [self.photosView addPhoto:image];
    
        // 退出imagePickerController
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.textView becomeFirstResponder];
    }
    
    • 系统自带的只能单选,实现多选要用大如图的库

  • 相关阅读:
    Windows自带Android模拟器启动失败
    Xamarin.Android提示找不到mono.Android.Support.v4
    Xamarin提示Build-tools版本过老
    Xamarin Android布局文件没有智能提示
    Xamarin.iOS模拟器调试找不到资源文件
    彻底卸载 RAD Studio 2009/2010/XE+ 的步骤
    Delphi版本号对照
    RAD Studio 2010 环境设置(转)
    C语言写的俄罗斯方块
    字符编解码的故事–ASCII,ANSI,Unicode,Utf-8区别
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5760189.html
Copyright © 2020-2023  润新知