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

iOS 开发调用相机以及获取相册照片功能

时间:2016-08-23 14:54:32      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

  //添加代理方法

@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>

 

 

//定义消息框

    UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选择相册",@"选择相机", nil];

    //显示消息框

    [act showInView:self.view];

 

//消息框代理实现

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    //定义图片选择器

    UIImagePickerController * picker = [[UIImagePickerController alloc]init];

    //判断

    switch (buttonIndex) {

        case 0:

            //判断系统是否允许选择 相册

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                //图片选择是相册(图片来源自相册)

                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                //设置代理

                picker.delegate=self;

                //模态显示界面

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

                

            }

            break;

        case 1://判断系统是否允许选择 相机

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                //图片选择是相册(图片来源自相册)

                picker.sourceType = UIImagePickerControllerSourceTypeCamera;

                //设置代理

                picker.delegate=self;

                //模态显示界面

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

            }

            else {

                NSLog(@"不支持相机");

            }

            

            

            break;

            

            

        default:

            break;

    }

}

 

 

//实现图片选择器代理

//参数:图片选择器  字典参数

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //通过key值获取到图片

    UIImage * image =info[UIImagePickerControllerOriginalImage];

    NSLog(@"image=%@  info=%@",image, info);

    

    //判断数据源类型

    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {

        

        //设置图片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次调用这个方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

 

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        NSLog(@"在相机中选择图片");

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        

        //设置图片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次调用这个方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

        

        [self dismissViewControllerAnimated:YES completion:nil];

    }

}

 

iOS 开发调用相机以及获取相册照片功能

标签:

原文地址:http://www.cnblogs.com/fantasy940155655/p/5799132.html

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