直接上代码
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];imagePickerController.delegate = self; //设置代理imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;[navigationController presentViewController:imagePickerController animated:YES completion:nil];}
}#pragma mark - UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {[picker dismissViewControllerAnimated:YES completion:^{}];UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //通过key值获取到图片self.avatarView.image = image;
}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {[picker dismissViewControllerAnimated:YES completion:^{}];
}
注意,弹出相册选择弹窗,不需要申请相册权限, 只有将图片写入系统相册
才需要这个权限,并且,弹出系统相册弹窗的时候,要添加这个判断
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {