demo地址
2017.8.1第二次更新
iOS 10之后,使用相机以及相册功能需要在plist文件中添加权限:
相机权限: Privacy - Camera Usage Description
相册权限: Privacy - Photo Library Usage Description
界面默认是英文界面需要修改infoPlist文件
Localization native development region = china
0.建立一个新的工具类,继承自NSObject
1.在.h文件中一个单例方法和一个开始照相方法
+ (instancetype)sharedManager;
- (void)startPhoto;
2.在.m文件中分别实现这两个方法
单例
+ (instancetype)sharedManager {
static GHPhoto *_instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
开始拍照
- (void)startPhoto {
if([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
_pickerController = [[UIImagePickerController alloc]init];
_pickerController.delegate = self;
_pickerController.allowsEditing=YES;
_pickerController.sourceType =UIImagePickerControllerSourceTypeCamera;
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:_pickerController animated:YES completion:nil];
}
}
3.在.m文件中遵守两个协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
4.在.m文件中实现相机的回调
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
/// 你得代码
[[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
5.在,h中声明一个block用来传出拍照完成的图片
typedef void (^FinishedBlock)(id object);
@property (nonatomic , copy)FinishedBlock Finishedblock;
6.在,m中实现这个block
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
/// 在这里拿到照片
// NSDate
NSString *img = info[@"UIImagePickerControllerOriginalImage"];
if (self.Finishedblock) {
self.Finishedblock(img);
}
// [self.currentVc dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
7.使用方法,在你要调用相机的地方添加如下代码
[[GHPhoto sharedManager] startPhoto];
8.在你想要拿到图片的地方添加如下代码
[GHPhoto sharedManager].Finishedblock = ^ (NSString *image) {
NSLog(@"我是图片");
};
网友评论