码迷,mamicode.com
首页 > 移动开发 > 详细

iOS拍摄照片

时间:2015-07-29 00:47:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

iOS拍摄照片

demo UI

添加一个UIImageView来显示图片,另外使用一个按键来进入拍照界面。

效果图如下:

技术分享

实现代码

1.设置代理,UIImagePickerController是UINavigationController的子类,所以UIImagePicker- Con-troller的委托也要遵守UINavigationControllerDelegate协议。

@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

2.点击按键时进入拍照界面

- (IBAction)takePicture:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    //检查相机设备是否可用,不可用时则使用本地图片库
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

3.在代理方法中显示拍照所得的图片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //显示图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

iOS拍摄照片

标签:

原文地址:http://www.cnblogs.com/limaofuyuanzhang/p/4684647.html

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