-(void)onUpload
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * firstAction = [UIAlertAction actionWithTitle:@"上传图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
//设置选取的照片是否可编辑
// pickerController.allowsEditing = YES;
//设置相册呈现的样式
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
//照片的选取样式还有以下两种
//UIImagePickerControllerSourceTypePhotoLibrary,直接全部呈现系统相册
//UIImagePickerControllerSourceTypeCamera//调取摄像头
// UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
//选择完成图片或者点击取消按钮都是通过代理来操作我们所需要的逻辑过程
pickerController.delegate = self;
//使用模态呈现相册
[self.navigationController presentViewController:pickerController animated:YES completion:nil];
}];
UIAlertAction * secondAction = [UIAlertAction actionWithTitle:@"上传视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.mediaTypes =@[(NSString*)kUTTypeMovie];
pickerController.allowsEditing = NO;
pickerController.delegate = self;
[self.navigationController presentViewController:pickerController animated:YES completion:nil];
}];
[alert addAction:firstAction];
[alert addAction:secondAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"%@",info);
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *mediaType = info[UIImagePickerControllerMediaType];
NSLog(@"%@",mediaType);
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
if (image!=nil) {
NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSLog(@"URL:%@",imageURL);
NSString *fileName = [imageURL lastPathComponent];
NSData * imageData = UIImageJPEGRepresentation(image,0.1);
// NSData * imageData = UIImagePNGRepresentation(image);
NSLog(@"压缩到0.1的图片大小:%lu",[imageData length]);
}
}else if([mediaType isEqualToString:(NSString*)kUTTypeMovie]){
NSLog(@"进入视频环节!!!");
NSInteger offset = 1024 * 1024 *5;
NSURL *URL = info[UIImagePickerControllerMediaURL];
NSString *URLStr = [NSString stringWithFormat:@"%@",URL];
NSData *file = [NSData dataWithContentsOfURL:URL];
NSLog(@"输出视频的大小:%lu",(unsigned long)[file length]);