ios照片获取、拍照功能-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

ios照片获取、拍照功能

简介:
//
//  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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章