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

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

时间:2015-01-16 11:26:25      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:ios   uiimagepickercontrol   

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

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


- (IBAction)pickPhotoLibrary:(id)sender {

    if (_imagePicker == nil) {

        _imagePicker = [[UIImagePickerController alloc] init];

    }

    _imagePicker.delegate = self;

    _imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

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

}


- (IBAction)pickPhotoCamera:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        if (_imagePicker == nil) {

            _imagePicker = [[UIImagePickerController alloc] init];

        }

        _imagePicker.delegate = self;

        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        

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

    } else {

        

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

    }

}



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

    _imagePicker.delegate = nil;

    

    [self dismissViewControllerAnimated:YES completion:nil];

}


- (void) imagePickerController: (UIImagePickerController *) picker

 didFinishPickingMediaWithInfo: (NSDictionary *) info {

    

    UIImage *originalImage = (UIImage *) [info objectForKey:

                                          UIImagePickerControllerOriginalImage];

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

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

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

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

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

     UIImagePickerControllerMediaURL;       //视频存放路径

     UIImagePickerControllerReferenceURL    //

     UIImagePickerControllerMediaMetadata    //

     */

    

    self.imageView.image = originalImage;

    //设置图片缩放填充

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;

    

    _imagePicker.delegate = nil;

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

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

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

标签:ios   uiimagepickercontrol   

原文地址:http://blog.csdn.net/u010670117/article/details/42773521

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