相机拍照,相册照片

xiaoxiao2021-02-28  176

首先,我们在头文件中添加需要用到的actionSheet控件,显示图片的UIImageView控件,并且加上所需要的协议

[objc] view plain copy #import <UIKit/UIKit.h>    @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>    @property (strongnonatomic) IBOutlet UIImageView *headImage;    @property (strongnonatomicUIActionSheet *actionSheet;    - (IBAction)clickPickImage:(id)sender;  @end  

通过点击我设置在界面中的按钮来呼出actionSheet控件,来选择相应的操作拍照或是在相册中选择相片,代码如下:

[objc] view plain copy //    #import "ImagePickerViewController.h"    @interface ImagePickerViewController ()    @end    @implementation ImagePickerViewController    @synthesize actionSheet = _actionSheet;    - (void)viewDidLoad {      [super viewDidLoad];      // Do any additional setup after loading the view from its nib.        }    - (void)didReceiveMemoryWarning {      [super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated.  }      /**  @ 调用ActionSheet  */  - (void)callActionSheetFunc{      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){          self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照"@"从相册选择", nil nil];      }else{          self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil nil];      }            self.actionSheet.tag = 1000;      [self.actionSheet showInView:self.view];  }    // Called when a button is clicked. The view will be automatically dismissed after this call returns  - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{      if (actionSheet.tag == 1000) {          NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;          // 判断是否支持相机          if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {              switch (buttonIndex) {                  case 0:                      //来源:相机                      sourceType = UIImagePickerControllerSourceTypeCamera;                      break;                  case 1:                      //来源:相册                      sourceType = UIImagePickerControllerSourceTypePhotoLibrary;                      break;                  case 2:                      return;              }          }          else {              if (buttonIndex == 2) {                  return;              } else {                  sourceType = UIImagePickerControllerSourceTypePhotoLibrary;              }          }          // 跳转到相机或相册页面          UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];          imagePickerController.delegate = self;          imagePickerController.allowsEditing = YES;          imagePickerController.sourceType = sourceType;                    [self presentViewController:imagePickerController animated:YES completion:^{                    }];      }  }    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  {      [picker dismissViewControllerAnimated:YES completion:^{            }];            UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];      self.headImage.image = image;  }    /* #pragma mark - Navigation  // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     // Get the new view controller using [segue destinationViewController].     // Pass the selected object to the new view controller. } */    - (IBAction)clickPickImage:(id)sender {            [self callActionSheetFunc];  }  @end  

///注意点

1.

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {

    UIImagePickerControllerSourceTypePhotoLibrary,

    UIImagePickerControllerSourceTypeCamera,

    UIImagePickerControllerSourceTypeSavedPhotosAlbum

};

  分别表示:图片列表,摄像头,相机相册 2.plist文件配置 3.打开相册,导航条是白色的,看不到取消的itme;

- (void)navigationController:(UINavigationController *)navigationController       willShowViewController:(UIViewController *)viewController                     animated:(BOOL)animated {     if ([navigationController isKindOfClass:[UIImagePickerController class]])     {         [viewController.navigationController.navigationBar setTintColor:[UIColor blackColor]];     } }

转载请注明原文地址: https://www.6miu.com/read-20180.html

最新回复(0)