博客> 关于iOS的照片获取方式(2种:照相和相册)
关于iOS的照片获取方式(2种:照相和相册)
2019-10-15 20:30 评论:0 阅读:19 李信念
ios

// // PhotoPickerManager.h // Latest // // Created by Lx on 16/4/12. // Copyright © 2016年 All rights reserved. //

import

import

//获取照片的回调 typedef void(^PickCompletionBlock)(UIImage *image);

//获取取消按钮的回调 typedef void(^PickCancelBlock)();

@interface PhotoPickerManager : NSObject

//用户选择使用照片 @property (nonatomic, copy) PickCompletionBlock completion;

//用户点击取消时的回调block @property (nonatomic, copy) PickCancelBlock cancelBlock;

+(PhotoPickerManager *)shared;

-(void)showAcitonSheetInView:(UIView )inView fromController:(UIViewController )fromController completion:(PickCompletionBlock)completion cancelBlock:(PickCancelBlock)cancelBlock;

@end

// // PhotoPickerManager.m // // // Created by k on 16/4/12. // Copyright © 2016年 All rights reserved. //

import "PhotoPickerManager.h"

define kScreenWidth [UIScreen mainScreen].bounds.size.width

define kScreenHeight [UIScreen mainScreen].bounds.size.height

define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES : NO)

@interface PhotoPickerManager ()

@property (nonatomic, weak) UIViewController *fromController;

@end

@implementation PhotoPickerManager

+(PhotoPickerManager )shared{ static PhotoPickerManager sharedObject = nil; static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
    if (!sharedObject) {
        sharedObject = [[[self class] alloc]init];;
    }
});
return sharedObject;

}

-(void)showAcitonSheetInView:(UIView )inView fromController:(UIViewController )fromController completion:(PickCompletionBlock)completion cancelBlock:(PickCancelBlock)cancelBlock{

self.completion = [completion copy];
self.cancelBlock = [cancelBlock copy];
self.fromController = fromController;

dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(globalQueue, ^{
    //不做是否能使用相机的判断了
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"拍照上传", nil];

    dispatch_async(dispatch_get_main_queue(), ^{
        [actionSheet showInView:inView];
    });
});

}

pragma mark - UIActionSheetDelegate

-(void)actionSheet:(UIActionSheet )actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { //从相册选择 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { UIImagePickerController picker = [[UIImagePickerController alloc]init]; picker.delegate =self; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType]; if (IOS8) { 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 isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        if (IOS8) {
            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];
    }
}

}

pragma mark -UIIamgePickerControllerDelegate

//选择了图片或者拍照了 -(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info{ [picker dismissViewControllerAnimated:YES completion:nil]; UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

if (image && self.completion) {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.fromController setNeedsStatusBarAppearanceUpdate];

    self.completion(image);
}

}

//取消 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil];

if (self.cancelBlock) {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.fromController setNeedsStatusBarAppearanceUpdate];
    self.cancelBlock();
}

}

@end

收藏
0
sina weixin mail 回到顶部