上传单张的情况
- (void)pickOne
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
//设置图片的来源
/*
UIImagePickerControllerSourceTypePhotoLibrary, 从照片应用的所有相册中选择一张图片
UIImagePickerControllerSourceTypeCamera, 利用照相机拍一张图片(如果需要自定义照相机,应该要使用AVFoundation.framework中的AVCaptureSession)
UIImagePickerControllerSourceTypeSavedPhotosAlbum 从照片应用的Moments相册中选择一张图片
*/
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
实现代理
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
\#pragma mark - <UIImagePickerControllerDelegate>
/**
* 图片选择完毕就会调用这个代理方法
*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSString *const UIImagePickerControllerMediaType;// 媒体类型
NSString *const UIImagePickerControllerOriginalImage;// 原始未编辑的图像
NSString *const UIImagePickerControllerEditedImage;// 编辑后的图像
NSString *const UIImagePickerControllerCropRect;// 源图像可编辑(有效?)区域
NSString *const UIImagePickerControllerMediaURL;// 视频的路径
NSString *const UIImagePickerControllerReferenceURL;// 原始选择项的URL
NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用摄像头并且是图像类型的时候有效.包含选择图像信息的字典类型复制代码
self.imageView.image = info[UIImagePickerControllerOriginalImage];
// 关闭选择图片的界面
[picker dismissViewControllerAnimated:YES completion:nil];
}
打印 info字典的key
上传多张:使用第三方框架 CTAssetsPickerController
https://github.com/chiunam/CTAssetsPickerController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// init picker
CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
// set delegate
picker.delegate = self;
// UI_USER_INTERFACE_IDIOM()返回当前设备的类型
// to present picker as a form sheet in iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
picker.modalPresentationStyle = UIModalPresentationFormSheet;
// present picker
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - <CTAssetsPickerControllerDelegate>
- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
[picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@", assets);
}