zoukankan      html  css  js  c++  java
  • ios 选择图片

    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);
        }
    }

  • 相关阅读:
    反射入门
    把数据库表的信息添加到list集合里面
    简单的事务分析及使用
    java-web与jdbc 的使用
    菜鸟入门bootstrap
    如何彻底的卸载mysql
    发生系统错误 1067,解决方案
    纯js的购物车案例
    idea里面怎么把自己项目添加maven
    js入门
  • 原文地址:https://www.cnblogs.com/shareze/p/4064259.html
Copyright © 2011-2022 走看看