-(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]);