首页 > 代码库 > UIImagePickerController(获取相册图片,打开相机)

UIImagePickerController(获取相册图片,打开相机)

UIImagePickerController的使用一定要在controller中,因为需要弹出控制器,如果不是在controller中,则可以通过响应者链查找获得controller,也可以通过代理方法回调

//本文查看地址http://write.blog.csdn.net/postedit


- (IBAction)pickPhotoLibrary:(id)sender {

   if (_imagePicker ==nil) {

        _imagePicker = [[UIImagePickerControlleralloc] init];

    }

    _imagePicker.delegate =self;

    _imagePicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

    [selfpresentViewController:_imagePickeranimated:YEScompletion:nil];

}


- (IBAction)pickPhotoCamera:(id)sender {

    if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

       if (_imagePicker ==nil) {

            _imagePicker = [[UIImagePickerControlleralloc] init];

        }

       _imagePicker.delegate =self;

        _imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;

        

        [selfpresentViewController:_imagePickeranimated:YEScompletion:nil];

    }else {

        

       NSLog(@"照相机不可用。");

    }

}



- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {

    _imagePicker.delegate =nil;

    

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


- (void) imagePickerController: (UIImagePickerController *) picker

 didFinishPickingMediaWithInfo: (NSDictionary *) info {

    

   UIImage *originalImage = (UIImage *) [infoobjectForKey:

                                         UIImagePickerControllerOriginalImage];

    /*-----------------------

     UIImagePickerControllerMediaType;    //由用户指定的媒体类型

     UIImagePickerControllerOriginalImage; //获取原始图片数据

     UIImagePickerControllerEditedImage;    //编辑后的图片数据

     UIImagePickerControllerCropRect;       //裁剪后的图片数据

     UIImagePickerControllerMediaURL;       //视频存放路径

     UIImagePickerControllerReferenceURL    //

     UIImagePickerControllerMediaMetadata    //

     */

    

   self.imageView.image = originalImage;

    //设置图片缩放填充

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;

    

    _imagePicker.delegate =nil;

    

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    

}

//文章属于个人笔记,严禁商业转载

UIImagePickerController(获取相册图片,打开相机)