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