文章目录
前言关于UINavigationControllerDelegate初始化UIImagePickerController卡顿检验相册是否可用返回NO的几种可能性
UIImagePickerControllerDelegate参考文章
前言
为了写图像识别,需要能通过相册和照相机获取照片,因此学习一下UIImagePickerController
关于UINavigationControllerDelegate
在使用UIImagePickerController时需要遵守<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
原因要看一下UIImagePickerController的定义
@interface UIImagePickerController : UINavigationController <NSCoding>
@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
UIImagePickerController本身就是继承于UINavigationController的同时delegate的申明里也写清楚了要遵守这两个协议猜测可能是因为 UIImagePickerController本身就设计了工具栏的变化
初始化UIImagePickerController卡顿
执行self.imagePickerController = [[UIImagePickerController alloc] init];初始化时,模拟机直接出现了3,4秒的卡顿在stack overflow上同样有人遇到这个问题UIImagePickerController really slow when calling alloc init高赞回答说只要demo不是debug model也就是不是通过Xcode运行就没事,经过测试,这是真的剩下的就很直接的异步就完事了所以最后觉得这一块改不改无所谓
检验相册是否可用
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType; // returns YES if source is available (i.e. camera present)
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
return;
}
//判断打开相册是否可用
返回NO的几种可能性
模拟机上没有照相机可以开启,需要使用真机在Info.plist里没有增加key没有锁定竖屏
UIImagePickerControllerDelegate
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
//销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil];
//设置图片
self.testImageView = [[UIImageView alloc] init];
_testImageView.image = info[UIImagePickerControllerOriginalImage];
NSLog(@"%@FAILQSTZZ", _testImageView.image);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(@"FAILQSTSD");
[self dismissViewControllerAnimated:YES completion:nil];
}
参考文章
ios基础篇(二十一)—— UIImagePickerController类iOS开发总结 - Xcode常见错误iOS 调用系统相机和相册iOS中获取系统相册中的图片