ios照片获取,拍照功能

时间:2021-09-30 23:38:02
  1. //
  2. //  HYBPhotoPickerManager.h
  3. //  ehui
  4. //
  5. //  Created by 黄仪标 on 14/11/26.
  6. //  Copyright (c) 2014年 黄仪标. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*!
  10. * @brief 照片获取或者拍照功能管理器
  11. * @author huangyibiao
  12. */
  13. @interface HYBPhotoPickerManager : NSObject
  14. + (HYBPhotoPickerManager *)shared;
  15. /*!
  16. * @brief 选择图片或者拍照完成选择使用拍照的图片后,会调用此block
  17. * @param image 选择的图片或者拍照后选择使用的图片
  18. */
  19. typedef void (^HYBPickerCompelitionBlock)(UIImage *image);
  20. /*!
  21. * @brief 用户点击取消时的回调block
  22. */
  23. typedef void (^HYBPickerCancelBlock)();
  24. /*!
  25. * @brief 此方法为调起选择图片或者拍照的入口,当选择图片或者拍照后选择使用图片后,回调completion,
  26. *        当用户点击取消后,回调cancelBlock
  27. * @param inView UIActionSheet呈现到inView这个视图上
  28. * @param fromController 用于呈现UIImagePickerController的控制器
  29. * @param completion 当选择图片或者拍照后选择使用图片后,回调completion
  30. * @param cancelBlock 当用户点击取消后,回调cancelBlock
  31. */
  32. - (void)showActionSheetInView:(UIView *)inView
  33. fromController:(UIViewController *)fromController
  34. completion:(HYBPickerCompelitionBlock)completion
  35. cancelBlock:(HYBPickerCancelBlock)cancelBlock;
  36. @end
    1. //
    2. //  HYBPhotoPickerManager.m
    3. //  ehui
    4. //
    5. //  Created by 黄仪标 on 14/11/26.
    6. //  Copyright (c) 2014年 黄仪标. All rights reserved.
    7. //
    8. #import "HYBPhotoPickerManager.h"
    9. #import "UIImagePickerController+Photo.h"
    10. #import "UIImage+DSResizeAndRound.h"
    11. @interface HYBPhotoPickerManager () <UIImagePickerControllerDelegate,
    12. UINavigationControllerDelegate,
    13. UIActionSheetDelegate>
    14. @property (nonatomic, weak)     UIViewController          *fromController;
    15. @property (nonatomic, copy)     HYBPickerCompelitionBlock completion;
    16. @property (nonatomic, copy)     HYBPickerCancelBlock      cancelBlock;
    17. @end
    18. @implementation HYBPhotoPickerManager
    19. + (HYBPhotoPickerManager *)shared {
    20. static HYBPhotoPickerManager *sharedObject = nil;
    21. static dispatch_once_t onceToken;
    22. dispatch_once(&onceToken, ^{
    23. if (!sharedObject) {
    24. sharedObject = [[[self class] alloc] init];
    25. }
    26. });
    27. return sharedObject;
    28. }
    29. - (void)showActionSheetInView:(UIView *)inView
    30. fromController:(UIViewController *)fromController
    31. completion:(HYBPickerCompelitionBlock)completion
    32. cancelBlock:(HYBPickerCancelBlock)cancelBlock {
    33. self.completion = [completion copy];
    34. self.cancelBlock = [cancelBlock copy];
    35. self.fromController = fromController;
    36. dispatch_async(kGlobalThread, ^{
    37. UIActionSheet *actionSheet = nil;
    38. if ([UIImagePickerController isCameraAvailable]) {
    39. actionSheet  = [[UIActionSheet alloc] initWithTitle:nil
    40. delegate:(id<UIActionSheetDelegate>)self
    41. cancelButtonTitle:@"取消"
    42. destructiveButtonTitle:nil
    43. otherButtonTitles:@"从相册选择", @"拍照上传", nil nil];
    44. } else {
    45. actionSheet = [[UIActionSheet alloc] initWithTitle:nil
    46. delegate:(id<UIActionSheetDelegate>)self
    47. cancelButtonTitle:@"取消"
    48. destructiveButtonTitle:nil
    49. otherButtonTitles:@"从相册选择", nil nil];
    50. }
    51. dispatch_async(kMainThread, ^{
    52. [actionSheet showInView:inView];
    53. });
    54. });
    55. return;
    56. }
    57. #pragma mark - UIActionSheetDelegate
    58. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    59. if (buttonIndex == 0) { // 从相册选择
    60. if ([UIImagePickerController isPhotoLibraryAvailable]) {
    61. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    62. picker.delegate = self;
    63. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    64. picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
    65. if (kIsIOS7OrLater) {
    66. picker.navigationBar.barTintColor = self.fromController.navigationController.navigationBar.barTintColor;
    67. }
    68. // 设置导航默认标题的颜色及字体大小
    69. picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
    70. NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    71. [self.fromController presentViewController:picker animated:YES completion:nil];
    72. }
    73. } else if (buttonIndex == 1) { // 拍照
    74. if ([UIImagePickerController canTakePhoto]) {
    75. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    76. picker.delegate = self;
    77. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    78. picker.delegate = self;
    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. }
    88. return;
    89. }
    90. #pragma mark - UIImagePickerControllerDelegate
    91. // 选择了图片或者拍照了
    92. - (void)imagePickerController:(UIImagePickerController *)aPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    93. [aPicker dismissViewControllerAnimated:YES completion:nil];
    94. __block UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    95. if (image && self.completion) {
    96. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    97. [self.fromController setNeedsStatusBarAppearanceUpdate];
    98. dispatch_async(kGlobalThread, ^{
    99. image = [image imageResizedToSize:CGSizeMake(kScreenWidth / 2.0, kScreenHeight / 2.0)];
    100. DDLogVerbose(@"image size : %@", NSStringFromCGSize(image.size));
    101. dispatch_async(kMainThread, ^{
    102. self.completion(image);
    103. });
    104. });
    105. }
    106. return;
    107. }
    108. // 取消
    109. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)aPicker {
    110. [aPicker dismissViewControllerAnimated:YES completion:nil];
    111. if (self.cancelBlock) {
    112. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    113. [self.fromController setNeedsStatusBarAppearanceUpdate];
    114. self.cancelBlock();
    115. }
    116. return;
    117. }
    118. @end