iOS笔记-UIImagePickerController使用小结

弹出UIImagePickerController(照片选择器)

  • 如何进入系统的相册UIImagePickerController?

    • 1.创建一个照片选择控制器
      UIImagePickerController *imagePickerVc = [[UIImagePickerController alloc] init];
    • 2.设置数据源(弹出的是否直接是照片库)
      imagePickerVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    • 3.设置代理(UIImagePickerControllerDelegate)
      imagePickerVc.delegate = self;
    • 4.把图片库Modal出来
      [self presentViewController:imagePickerVc animated:YES completion:nil];
  • 如何监听并处理弹出来照片库的点击事件?

    • 1.遵守代理协议
      @interface ViewController ()< UIImagePickerControllerDelegate>
    • 2.设置代理为self
      imagePickerVc.delegate = self;
    • 3.实现代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 获取用户选择照片
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    //把Modal出来的imagePickerController弹出
    [self dismissViewControllerAnimated:YES completion:nil];

    // 在此处理图片
}

你可能感兴趣的:(iOS笔记-UIImagePickerController使用小结)