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

ios 选择图片

时间:2014-10-31 09:57:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   io   os   ar   for   sp   on   cti   bs   

1.控制器实现代理:
  UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate

2.对图片添加手势:
  UITapGestureRecognizer *gs=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(choicePic)];
    [picImageView addGestureRecognizer:gs];
    picImageView.userInteractionEnabled=YES;

3.实现手势对应方法:
   UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:SHEET_PIC_SOURCE_TITLE delegate:self cancelButtonTitle:SHEET_PIC_SOURCE_CACEL destructiveButtonTitle:nil otherButtonTitles:SHEET_PIC_SOURCE_CAMERA,SHEET_PIC_SOURCE_PHOTO, nil];
    [sheet showInView:self.view];

4.实现sheet对应代理方法:
  -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate=self;
    if(buttonIndex==0){
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
             picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }else{
            [UtilTool ShowAlertView:ALERT_WARN_TITLE setMsg:OPEN_CAMERA_ERRORMSG];
        }
        [self presentViewController:picker animated:YES completion:nil];
    }else if (buttonIndex==1){
        picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }else{
    }
}

5.实现选择照片或照相机代理方法:
  -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [UIApplication sharedApplication].statusBarHidden=NO;
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if([mediaType isEqualToString:@"public.image"]){
        UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
        
        picImageView.image=image;
        

   //目的:判断是否选择了图片,可以简单判断是否需要上传图片
        //isPic=1;
        
        if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
}

ios 选择图片

标签:des   io   os   ar   for   sp   on   cti   bs   

原文地址:http://www.cnblogs.com/shareze/p/4064259.html

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