相机拍照,相册照片

来源:互联网 发布:应用统计学大数据方向 编辑:程序博客网 时间:2024/05/01 09:21
首先,我们在头文件中添加需要用到的actionSheet控件,显示图片的UIImageView控件,并且加上所需要的协议

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>  
  4.   
  5. @property (strongnonatomic) IBOutlet UIImageView *headImage;  
  6.   
  7. @property (strongnonatomicUIActionSheet *actionSheet;  
  8.   
  9. - (IBAction)clickPickImage:(id)sender;  
  10. @end  

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

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //  
  2.   
  3. #import "ImagePickerViewController.h"  
  4.   
  5. @interface ImagePickerViewController ()  
  6.   
  7. @end  
  8.   
  9. @implementation ImagePickerViewController  
  10.   
  11. @synthesize actionSheet = _actionSheet;  
  12.   
  13. - (void)viewDidLoad {  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view from its nib.  
  16.       
  17. }  
  18.   
  19. - (void)didReceiveMemoryWarning {  
  20.     [super didReceiveMemoryWarning];  
  21.     // Dispose of any resources that can be recreated.  
  22. }  
  23.   
  24.   
  25. /** 
  26.  @ 调用ActionSheet 
  27.  */  
  28. - (void)callActionSheetFunc{  
  29.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){  
  30.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照"@"从相册选择", nil nil];  
  31.     }else{  
  32.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil nil];  
  33.     }  
  34.       
  35.     self.actionSheet.tag = 1000;  
  36.     [self.actionSheet showInView:self.view];  
  37. }  
  38.   
  39. // Called when a button is clicked. The view will be automatically dismissed after this call returns  
  40. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  41.     if (actionSheet.tag == 1000) {  
  42.         NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  43.         // 判断是否支持相机  
  44.         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  45.             switch (buttonIndex) {  
  46.                 case 0:  
  47.                     //来源:相机  
  48.                     sourceType = UIImagePickerControllerSourceTypeCamera;  
  49.                     break;  
  50.                 case 1:  
  51.                     //来源:相册  
  52.                     sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  53.                     break;  
  54.                 case 2:  
  55.                     return;  
  56.             }  
  57.         }  
  58.         else {  
  59.             if (buttonIndex == 2) {  
  60.                 return;  
  61.             } else {  
  62.                 sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  63.             }  
  64.         }  
  65.         // 跳转到相机或相册页面  
  66.         UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];  
  67.         imagePickerController.delegate = self;  
  68.         imagePickerController.allowsEditing = YES;  
  69.         imagePickerController.sourceType = sourceType;  
  70.           
  71.         [self presentViewController:imagePickerController animated:YES completion:^{  
  72.           
  73.         }];  
  74.     }  
  75. }  
  76.   
  77. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  78. {  
  79.     [picker dismissViewControllerAnimated:YES completion:^{  
  80.       
  81.     }];  
  82.       
  83.     UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];  
  84.     self.headImage.image = image;  
  85. }  
  86.   
  87. /* 
  88. #pragma mark - Navigation 
  89.  
  90. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  91. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  92.     // Get the new view controller using [segue destinationViewController]. 
  93.     // Pass the selected object to the new view controller. 
  94. } 
  95. */  
  96.   
  97. - (IBAction)clickPickImage:(id)sender {  
  98.       
  99.     [self callActionSheetFunc];  
  100. }  
  101. @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]];
    }
}

0 0
原创粉丝点击