ios照片获取、拍照功能

时间:2022-06-16 13:32:39
//
//  HYBPhotoPickerManager.h
//  ehui
//
//  Created by 黄仪标 on 14/11/26.
//  Copyright (c) 2014年 黄仪标. All rights reserved.
//

#import <Foundation/Foundation.h>

/*!
 * @brief 照片获取或者拍照功能管理器
 * @author huangyibiao
 */
@interface HYBPhotoPickerManager : NSObject 

+ (HYBPhotoPickerManager *)shared;


/*!
 * @brief 选择图片或者拍照完成选择使用拍照的图片后,会调用此block
 * @param image 选择的图片或者拍照后选择使用的图片
 */
typedef void (^HYBPickerCompelitionBlock)(UIImage *image);
/*!
 * @brief 用户点击取消时的回调block
 */
typedef void (^HYBPickerCancelBlock)();

/*!
 * @brief 此方法为调起选择图片或者拍照的入口,当选择图片或者拍照后选择使用图片后,回调completion,
 *        当用户点击取消后,回调cancelBlock
 * @param inView UIActionSheet呈现到inView这个视图上
 * @param fromController 用于呈现UIImagePickerController的控制器
 * @param completion 当选择图片或者拍照后选择使用图片后,回调completion
 * @param cancelBlock 当用户点击取消后,回调cancelBlock
 */
- (void)showActionSheetInView:(UIView *)inView
               fromController:(UIViewController *)fromController
                   completion:(HYBPickerCompelitionBlock)completion
                  cancelBlock:(HYBPickerCancelBlock)cancelBlock;

@end


//
//  HYBPhotoPickerManager.m
//  ehui
//
//  Created by 黄仪标 on 14/11/26.
//  Copyright (c) 2014年 黄仪标. All rights reserved.
//

#import "HYBPhotoPickerManager.h"
#import "UIImagePickerController+Photo.h"
#import "UIImage+DSResizeAndRound.h"

@interface HYBPhotoPickerManager () <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIActionSheetDelegate>

@property (nonatomic, weak)     UIViewController          *fromController;
@property (nonatomic, copy)     HYBPickerCompelitionBlock completion;
@property (nonatomic, copy)     HYBPickerCancelBlock      cancelBlock;

@end

@implementation HYBPhotoPickerManager

+ (HYBPhotoPickerManager *)shared {
  static HYBPhotoPickerManager *sharedObject = nil;
  static dispatch_once_t onceToken;
  
  dispatch_once(&onceToken, ^{
    if (!sharedObject) {
      sharedObject = [[[self class] alloc] init];
    }
  });
  
  return sharedObject;
}

- (void)showActionSheetInView:(UIView *)inView
               fromController:(UIViewController *)fromController
                   completion:(HYBPickerCompelitionBlock)completion
                  cancelBlock:(HYBPickerCancelBlock)cancelBlock {
  self.completion = [completion copy];
  self.cancelBlock = [cancelBlock copy];
  self.fromController = fromController;
  
  dispatch_async(kGlobalThread, ^{
    UIActionSheet *actionSheet = nil;
    if ([UIImagePickerController isCameraAvailable]) {
      actionSheet  = [[UIActionSheet alloc] initWithTitle:nil
                                                 delegate:(id<UIActionSheetDelegate>)self
                                        cancelButtonTitle:@"取消"
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:@"从相册选择", @"拍照上传", nil];
    } else {
      actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                delegate:(id<UIActionSheetDelegate>)self
                                       cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                       otherButtonTitles:@"从相册选择", nil];
    }
    
    dispatch_async(kMainThread, ^{
      [actionSheet showInView:inView];
    });
  });
  
  return;
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0) { // 从相册选择
    if ([UIImagePickerController isPhotoLibraryAvailable]) {
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
      picker.delegate = self;
      picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
      
      if (kIsIOS7OrLater) {
        picker.navigationBar.barTintColor = self.fromController.navigationController.navigationBar.barTintColor;
      }
      // 设置导航默认标题的颜色及字体大小
      picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                                   NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
      [self.fromController presentViewController:picker animated:YES completion:nil];
    }
  } else if (buttonIndex == 1) { // 拍照
    if ([UIImagePickerController canTakePhoto]) {
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
      picker.delegate = self;
      picker.sourceType = UIImagePickerControllerSourceTypeCamera;
      picker.delegate = self;
      if (kIsIOS7OrLater) {
        picker.navigationBar.barTintColor = self.fromController.navigationController.navigationBar.barTintColor;
      }
      // 设置导航默认标题的颜色及字体大小
      picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                                   NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
      [self.fromController presentViewController:picker animated:YES completion:nil];
    }
  }
  return;
}

#pragma mark - UIImagePickerControllerDelegate
// 选择了图片或者拍照了
- (void)imagePickerController:(UIImagePickerController *)aPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  [aPicker dismissViewControllerAnimated:YES completion:nil];
  __block UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
  
  if (image && self.completion) {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.fromController setNeedsStatusBarAppearanceUpdate];
    
    dispatch_async(kGlobalThread, ^{
      image = [image imageResizedToSize:CGSizeMake(kScreenWidth / 2.0, kScreenHeight / 2.0)];
      DDLogVerbose(@"image size : %@", NSStringFromCGSize(image.size));
      
      dispatch_async(kMainThread, ^{
        self.completion(image);
      });
    });
  }
  return;
}

// 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)aPicker {
  [aPicker dismissViewControllerAnimated:YES completion:nil];
  
  if (self.cancelBlock) {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.fromController setNeedsStatusBarAppearanceUpdate];
    
    self.cancelBlock();
  }
  return;
}

@end