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

UIImagePickerController---图片添加---初级使用

时间:2015-07-15 20:54:33      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

1.创建UIImageView

-(void)creatPhotoImageView
{
    self.photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 80, 80)];
    self.photoImageView.backgroundColor = [UIColor blackColor];
    // 打开用户交互(默认关闭)
    self.photoImageView.userInteractionEnabled = YES;
    [self addSubview:self.photoImageView];
}
2.在创建的UIImageView上添加轻拍手势

    // 轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
    // 添加手势
    [self.rootView.photoImageView addGestureRecognizer:tapGR];

2.1手势方法---创建UIActionSheet---设置代理(遵守代理协议)

- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取",@"拍照", nil];
    [sheet showInView:self.rootView];
    NSLog(@"%ld",sheet.cancelButtonIndex);
}

3.UIActionSheetDelegate协议代理方法---对应的按钮添加事件---给创建的UIImagePickerController设置代理(遵守代理协议UINavigationControllerDelegate,UIImagePickerControllerDelegate)

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

    NSLog(@"%ld",buttonIndex);
    if (buttonIndex == actionSheet.firstOtherButtonIndex) {
            // 检测照片源是否可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            
            UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
            
            // 通过代理方法拿到图片
            imagePickerVC.delegate = self;
            // 编辑设置默认no  代理方法key为UIImagePickerControllerEditedImage时 必须设置为YES
            imagePickerVC.allowsEditing = YES;
            
            // 指定imagePickerVC从相册获取
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            // 模态一个控制器
            [self presentViewController:imagePickerVC animated:YES completion:nil];
        }
    }else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1){
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
            imagePickerVC.delegate = self;
            imagePickerVC.allowsEditing = YES;
            
            // 指定imagePickerVC从相机中获取
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePickerVC animated:YES completion:nil];
        }
    } 
}

4.UINavigationControllerDelegate,UIImagePickerControllerDelegate协议方法---取消模态---取出图片---给对应的UIImageView设置图片

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

    // picker消失 (模态消失)
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 根据定好的key值取出图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    self.rootView.photoImageView.image = image;
    
}

这就完成了给一个UIImageView从相册或者自拍照中添加图片

UIImagePickerController---图片添加---初级使用

标签:

原文地址:http://www.cnblogs.com/bachl/p/4649276.html

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