美文网首页
从相册中获取图片

从相册中获取图片

作者: 金歌漫舞 | 来源:发表于2016-08-07 17:58 被阅读16次

    1.利用UIImagePickeController可以从系统自带的App(照片,相册)中获取图片
    2.设置代理,准守代理协议
    *****注:UIImagePickerController类比较特殊,需要准守两个代理协议;

    @interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>  
    

    3.实现代理的方法

    #pragma mark  
    #pragma mark - imagePicker的 代理方法  
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info  
    
    <span style="color:#333333;">- (IBAction)photo:(id)sender </span><pre name="code" class="objc">{  
        // 1.判断相册是否可以打开  
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;  
        // 2. 创建图片选择控制器  
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];  
        /** 
         typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { 
         UIImagePickerControllerSourceTypePhotoLibrary, // 相册 
         UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取 
         UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿 
         } 
         */  
        // 3. 设置打开照片相册类型(显示所有相簿)    
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
        // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
        // 照相机  
        // ipc.sourceType = UIImagePickerControllerSourceTypeCamera;  
        // 4.设置代理  
        ipc.delegate = self;  
        // 5.modal出这个控制器  
        [self presentViewController:ipc animated:YES completion:nil];  
    }  
    
    <span style="color:#333333;"></span><pre name="code" class="objc">#pragma mark -- <UIImagePickerControllerDelegate>--  
    // 获取图片后的操作  
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info  
    {  
        // 销毁控制器  
        [picker dismissViewControllerAnimated:YES completion:nil];  
      
        // 设置图片  
        self.imageView.image = info[UIImagePickerControllerOriginalImage];  
    }  
    

    相关文章

      网友评论

          本文标题:从相册中获取图片

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