UIImagePicker授权判断

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:nil];

    

    // 照相机拍照返回后无法滑动到最底部,原因未知

    CGPoint currentOffset = self.mainTableView.contentOffset;

    currentOffset.y += 20;

    [self.mainTableView setContentOffset:currentOffset animated:YES];

}

 

- (IBAction)cameraClicked:(id)sender {

    UIImagePickerController *pick = [[UIImagePickerController alloc] init];

   

    // 判断当前设备相机是否可用

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        

        pick.sourceType = UIImagePickerControllerSourceTypeCamera;

//        pick.allowsEditing = YES;

        pick.delegate = self;

        

        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

        

        switch (status) {

            case AVAuthorizationStatusAuthorized:{

                // 已授权

                DLog(@"已授权使用相机")

            }

                break;

                

            case AVAuthorizationStatusNotDetermined:{

                // 第一次调用此方法时,系统会提示用户授权,再次调用则不会提醒用户,而会直接传递之前选择的值

                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {

                    // 用户选择了不允许

                    if (!granted) {

                        DLog(@"用户未允许")

                    }

                }];

            }

                break;

                

            default:{

                // 未授权

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请在iPhone的“设置-隐私-相机”选项中,允许才宝访问你的相机" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];

                

                [alert show];

            }

                break;

        }

        [self presentViewController:pick animated:YES completion:nil];

    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前设备相机不可用" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        

        [alert show];

    }

}

 

 

#pragma mark - HMImagePickerControllerDelegate

- (void)imagePickerController:(HMImagePickerController *)picker

      didFinishSelectedImages:(NSArray<UIImage *> *)images

               selectedAssets:(NSArray<PHAsset *> *)selectedAssets {

    // 记录图像,方便显示

    self.images = images;

    

    // 记录选中资源集合,方便再次选择照片定位

    //    self.selectedAssets = selectedAssets;

    

    for (UIImage *image in images) {

        [self sendImage:image];

    }

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

    // 隐藏底部选择视图

    [self.selectImageButton sendActionsForControlEvents:UIControlEventTouchUpInside];

}

 

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    

    // 若为相机拍摄的则保存到相册中

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){

        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

    }

    

    image = [ToolUtils fixOrientation:image];

    

    // 发送图片

    [self sendImage:image];

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

    // 隐藏底部选择视图

    [self.selectImageButton sendActionsForControlEvents:UIControlEventTouchUpInside];

    

    // 照相机拍照返回后无法滑动到最底部,原因未知

    CGPoint currentOffset = self.mainTableView.contentOffset;

    currentOffset.y += 20;

    [self.mainTableView setContentOffset:currentOffset animated:YES];

}

 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:nil];

    

    // 照相机拍照返回后无法滑动到最底部,原因未知

    CGPoint currentOffset = self.mainTableView.contentOffset;

    currentOffset.y += 20;

    [self.mainTableView setContentOffset:currentOffset animated:YES];

}

 

 

- (IBAction)imageClicked:(id)sender {

    HMImagePickerController *picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];

    

    picker.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};

    picker.navigationBar.tintColor = [UIColor whiteColor];

    

    // 设置图像选择代理

    picker.hmpickerDelegate = self;

    // 设置目标图片尺寸

    picker.targetSize = CGSizeMake(600, 600);

    // 设置最大选择照片数量

    picker.maxPickerCount = 5;

    

    [self presentViewController:picker animated:YES completion:nil];

}

第三方重写

头文件

#import "TZImagePickerController.h"

 

@protocol HMImagePickerControllerDelegate;

 

/// 图像选择控制器

@interface HMImagePickerController : TZImagePickerController

 

/// 图像选择代理

@property (nonatomic, weak, nullable) id<HMImagePickerControllerDelegate> hmpickerDelegate;

/// 加载图像尺寸(以像素为单位,默认大小 600 * 600)

@property (nonatomic) CGSize targetSize;

/// 最大选择图像数量,默认 9 张

@property (nonatomic) NSInteger maxPickerCount;

 

/// 构造函数

///

/// @param selectedAssets 选中素材数组,可以用于预览之前选中的照片集合

///

/// @return 图像选择控制器

- (_Nonnull instancetype)initWithSelectedAssets:(NSArray <PHAsset *> * _Nullable)selectedAssets;

 

 

@end

 

 

/// 图像选择控制器协议

@protocol HMImagePickerControllerDelegate <NSObject>

@optional

/// 图像选择完成代理方法

///

/// @param picker         图像选择控制器

/// @param images         用户选中图像数组

/// @param selectedAssets 选中素材数组,方便重新定位图像

- (void)imagePickerController:(HMImagePickerController * _Nonnull)picker

      didFinishSelectedImages:(NSArray <UIImage *> * _Nonnull)images

               selectedAssets:(NSArray <PHAsset *> * _Nullable)selectedAssets;

@end

实现文件

#import "HMImagePickerController.h"

 

@interface HMImagePickerController ()

 

@end

 

@implementation HMImagePickerController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}

 

- (_Nonnull instancetype)initWithSelectedAssets:(NSArray <PHAsset *> * _Nullable)selectedAssets {

    

    self = [super init];

    

    if (self) {

        

        //    imagePickerVc.selectedAssets = _selectedAssets; // 是否过滤掉已选择的图片

        self.allowTakePicture = NO; // 在内部显示拍照按钮

        self.allowTakeVideo = NO;   // 在内部显示拍视频按

        

        // 导出图片的宽度,默认828像素宽

        //     imagePickerVc.photoWidth = 1000;

        

        // 2. Set the appearance

        // 2. 在这里设置imagePickerVc的外观

         self.navigationBar.barTintColor = APPColor;

        // imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];

         self.oKButtonTitleColorNormal = [UIColor greenColor];

        // imagePickerVc.navigationBar.translucent = NO;

        self.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];

        self.showPhotoCannotSelectLayer = YES;

        self.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];

        [self setPhotoPickerPageUIConfigBlock:^(UICollectionView *collectionView, UIView *bottomToolBar, UIButton *previewButton, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel, UIView *divideLine) {

            [doneButton setTitleColor:APPColor forState:UIControlStateNormal];

        }];

        

        // 3. Set allow picking video & photo & originalPhoto or not

        // 3. 设置是否可以选择视频/图片/原图

        self.allowPickingVideo = NO;

        self.allowPickingImage = YES;

        self.allowPickingOriginalPhoto = NO;

        self.allowPickingGif = NO;

        self.allowPickingMultipleVideo = NO; // 是否可以多选视频

        

        // 4. 照片排列按修改时间升序

        self.sortAscendingByModificationDate = YES;

        

        self.statusBarStyle = UIStatusBarStyleLightContent;

        

        // 设置是否显示图片序号

        self.showSelectedIndex = YES;

        

#pragma mark - 到这里为止

        

        // You can get the photos by block, the same as by delegate.

        __weak typeof(self)weakSelf = self;

        // 你可以通过block或者代理,来得到用户选择的照片.

        [self setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {

            

            if ([weakSelf.hmpickerDelegate respondsToSelector:@selector(imagePickerController:didFinishSelectedImages:selectedAssets:)]) {

                [weakSelf.hmpickerDelegate imagePickerController:weakSelf didFinishSelectedImages:photos selectedAssets:assets];

            }

        }];

        

    }

    

    return self;

}

 

- (void)setTargetSize:(CGSize)targetSize {

    

    _targetSize = targetSize;

    self.photoWidth = targetSize.width;

}

 

- (void)setMaxPickerCount:(NSInteger)maxPickerCount {

    _maxPickerCount = maxPickerCount;

    self.maxImagesCount = maxPickerCount;

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

@end

posted @ 2018-12-19 13:15  sundaysios  阅读(358)  评论(0)    收藏  举报