ios照片获取,拍照功能

 

  1. //  
  2. //  HYBPhotoPickerManager.h  
  3. //  ehui  
  4. //  
  5. //  Created by 黄仪标 on 14/11/26.  
  6. //  Copyright (c) 2014年 黄仪标. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. /*! 
  12.  * @brief 照片获取或者拍照功能管理器 
  13.  * @author huangyibiao 
  14.  */  
  15. @interface HYBPhotoPickerManager : NSObject   
  16.   
  17. + (HYBPhotoPickerManager *)shared;  
  18.   
  19.   
  20. /*! 
  21.  * @brief 选择图片或者拍照完成选择使用拍照的图片后,会调用此block 
  22.  * @param image 选择的图片或者拍照后选择使用的图片 
  23.  */  
  24. typedef void (^HYBPickerCompelitionBlock)(UIImage *image);  
  25. /*! 
  26.  * @brief 用户点击取消时的回调block 
  27.  */  
  28. typedef void (^HYBPickerCancelBlock)();  
  29.   
  30. /*! 
  31.  * @brief 此方法为调起选择图片或者拍照的入口,当选择图片或者拍照后选择使用图片后,回调completion, 
  32.  *        当用户点击取消后,回调cancelBlock 
  33.  * @param inView UIActionSheet呈现到inView这个视图上 
  34.  * @param fromController 用于呈现UIImagePickerController的控制器 
  35.  * @param completion 当选择图片或者拍照后选择使用图片后,回调completion 
  36.  * @param cancelBlock 当用户点击取消后,回调cancelBlock 
  37.  */  
  38. - (void)showActionSheetInView:(UIView *)inView  
  39.                fromController:(UIViewController *)fromController  
  40.                    completion:(HYBPickerCompelitionBlock)completion  
  41.                   cancelBlock:(HYBPickerCancelBlock)cancelBlock;  
  42.   
  43. @end  

 

 

    1. //  
    2. //  HYBPhotoPickerManager.m  
    3. //  ehui  
    4. //  
    5. //  Created by 黄仪标 on 14/11/26.  
    6. //  Copyright (c) 2014年 黄仪标. All rights reserved.  
    7. //  
    8.   
    9. #import "HYBPhotoPickerManager.h"  
    10. #import "UIImagePickerController+Photo.h"  
    11. #import "UIImage+DSResizeAndRound.h"  
    12.   
    13. @interface HYBPhotoPickerManager () <UIImagePickerControllerDelegate,  
    14. UINavigationControllerDelegate,  
    15. UIActionSheetDelegate>  
    16.   
    17. @property (nonatomic, weak)     UIViewController          *fromController;  
    18. @property (nonatomic, copy)     HYBPickerCompelitionBlock completion;  
    19. @property (nonatomic, copy)     HYBPickerCancelBlock      cancelBlock;  
    20.   
    21. @end  
    22.   
    23. @implementation HYBPhotoPickerManager  
    24.   
    25. + (HYBPhotoPickerManager *)shared {  
    26.   static HYBPhotoPickerManager *sharedObject = nil;  
    27.   static dispatch_once_t onceToken;  
    28.     
    29.   dispatch_once(&onceToken, ^{  
    30.     if (!sharedObject) {  
    31.       sharedObject = [[[self class] alloc] init];  
    32.     }  
    33.   });  
    34.     
    35.   return sharedObject;  
    36. }  
    37.   
    38. - (void)showActionSheetInView:(UIView *)inView  
    39.                fromController:(UIViewController *)fromController  
    40.                    completion:(HYBPickerCompelitionBlock)completion  
    41.                   cancelBlock:(HYBPickerCancelBlock)cancelBlock {  
    42.   self.completion = [completion copy];  
    43.   self.cancelBlock = [cancelBlock copy];  
    44.   self.fromController = fromController;  
    45.     
    46.   dispatch_async(kGlobalThread, ^{  
    47.     UIActionSheet *actionSheet = nil;  
    48.     if ([UIImagePickerController isCameraAvailable]) {  
    49.       actionSheet  = [[UIActionSheet alloc] initWithTitle:nil  
    50.                                                  delegate:(id<UIActionSheetDelegate>)self  
    51.                                         cancelButtonTitle:@"取消"  
    52.                                    destructiveButtonTitle:nil  
    53.                                         otherButtonTitles:@"从相册选择", @"拍照上传", nil nil];  
    54.     } else {  
    55.       actionSheet = [[UIActionSheet alloc] initWithTitle:nil  
    56.                                                 delegate:(id<UIActionSheetDelegate>)self  
    57.                                        cancelButtonTitle:@"取消"  
    58.                                   destructiveButtonTitle:nil  
    59.                                        otherButtonTitles:@"从相册选择", nil nil];  
    60.     }  
    61.       
    62.     dispatch_async(kMainThread, ^{  
    63.       [actionSheet showInView:inView];  
    64.     });  
    65.   });  
    66.     
    67.   return;  
    68. }  
    69.   
    70. #pragma mark - UIActionSheetDelegate  
    71. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  
    72.   if (buttonIndex == 0) { // 从相册选择  
    73.     if ([UIImagePickerController isPhotoLibraryAvailable]) {  
    74.       UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
    75.       picker.delegate = self;  
    76.       picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
    77.       picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];  
    78.         
    79.       if (kIsIOS7OrLater) {  
    80.         picker.navigationBar.barTintColor = self.fromController.navigationController.navigationBar.barTintColor;  
    81.       }  
    82.       // 设置导航默认标题的颜色及字体大小  
    83.       picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],  
    84.                                                    NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};  
    85.       [self.fromController presentViewController:picker animated:YES completion:nil];  
    86.     }  
    87.   } else if (buttonIndex == 1) { // 拍照  
    88.     if ([UIImagePickerController canTakePhoto]) {  
    89.       UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
    90.       picker.delegate = self;  
    91.       picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    92.       picker.delegate = self;  
    93.       if (kIsIOS7OrLater) {  
    94.         picker.navigationBar.barTintColor = self.fromController.navigationController.navigationBar.barTintColor;  
    95.       }  
    96.       // 设置导航默认标题的颜色及字体大小  
    97.       picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],  
    98.                                                    NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};  
    99.       [self.fromController presentViewController:picker animated:YES completion:nil];  
    100.     }  
    101.   }  
    102.   return;  
    103. }  
    104.   
    105. #pragma mark - UIImagePickerControllerDelegate  
    106. // 选择了图片或者拍照了  
    107. - (void)imagePickerController:(UIImagePickerController *)aPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {  
    108.   [aPicker dismissViewControllerAnimated:YES completion:nil];  
    109.   __block UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];  
    110.     
    111.   if (image && self.completion) {  
    112.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  
    113.     [self.fromController setNeedsStatusBarAppearanceUpdate];  
    114.       
    115.     dispatch_async(kGlobalThread, ^{  
    116.       image = [image imageResizedToSize:CGSizeMake(kScreenWidth / 2.0, kScreenHeight / 2.0)];  
    117.       DDLogVerbose(@"image size : %@", NSStringFromCGSize(image.size));  
    118.         
    119.       dispatch_async(kMainThread, ^{  
    120.         self.completion(image);  
    121.       });  
    122.     });  
    123.   }  
    124.   return;  
    125. }  
    126.   
    127. // 取消  
    128. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)aPicker {  
    129.   [aPicker dismissViewControllerAnimated:YES completion:nil];  
    130.     
    131.   if (self.cancelBlock) {  
    132.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  
    133.     [self.fromController setNeedsStatusBarAppearanceUpdate];  
    134.       
    135.     self.cancelBlock();  
    136.   }  
    137.   return;  
    138. }  
    139.   
    140. @end 
posted @ 2016-04-20 13:50  Jack~Bai  阅读(594)  评论(0编辑  收藏  举报