UIImagePickerController學習記錄

前言

  • 爲了寫圖像識別,需要能通過相冊和照相機獲取照片,因此學習一下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];
}

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章