#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//上传附件
- (void)btnUploadClick
{
UIActionSheet *as=[[UIActionSheet alloc]initWithTitle:@"上传附件" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"马上照一张或录一段视频" otherButtonTitles:@"从相册中选择照片或视频", nil ];
as.actionSheetStyle=2;
[as showInView:[[[UIApplication sharedApplication] delegate] window]];
}
//MARK:用户头像上传的方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
DLog(@"%ld",buttonIndex);
switch (buttonIndex) {
case 0:
{
UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
[imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imgPicker.delegate = self;
[imgPicker setAllowsEditing:YES];
imgPicker.navigationBar.barTintColor = [UIColor MyNavigationbarColorSelection];imgPicker.navigationBar.barTintColor = [UIColor MyNavigationbarColorSelection];
NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"
// imgPicker.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie
imgPicker.mediaTypes = availableMedia;
[self presentViewController:imgPicker animated:YES completion:^{
}];
}
//照一张
break;
case 1:
//搞一张
{
UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
[imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imgPicker setDelegate:self];
[imgPicker setAllowsEditing:YES];
imgPicker.navigationBar.barTintColor = [UIColor MyNavigationbarColorSelection];
NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"
// imgPicker.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie
imgPicker.mediaTypes = availableMedia;
imgPicker.videoMaximumDuration = 60.0f;//60秒
[self presentViewController:imgPicker animated:YES completion:^{
}];
break;
}
default:
break;
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
DLog(@"点击了取消");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
DLog(@"------>%@",info);
//模态消失
[picker dismissViewControllerAnimated:YES completion:nil];
if ([[NSString stringWithFormat:@"%@",[info objectForKey:@"UIImagePickerControllerMediaType"]] isEqualToString:@"public.image"]) {//图片
UIImage *image;
if (picker.allowsEditing)
{
image = [info objectForKey:UIImagePickerControllerEditedImage];
}else
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
// NSString *aPath = [NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];//生成路径
NSData *imgData = UIImageJPEGRepresentation(image, 0.1);// 压缩图片 参数0-1 1最大 0无限接近 最小15k左右
// [imgData writeToFile:aPath atomically:YES];
[SVProgressHUD showWithStatus:@"上传图片中"];
[Manage requestToUploadAttachmentsDataWith:^(NSDictionary *base) {
if ([base[@"errno"] intValue] == 200) {
[SVProgressHUD showInfoWithStatus:[base objectForKey:@"err"]];
}else{
[SVProgressHUD showInfoWithStatus:[base objectForKey:@"err"]];
}
} taskId:@"" fileImg:imgData URL:nil enclosureType:@"demand" fileVideo:nil];
}else if ([[NSString stringWithFormat:@"%@",[info objectForKey:@"UIImagePickerControllerMediaType"]] isEqualToString:@"public.movie"]){//视频
NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
DLog(@"%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:sourceURL]]);
DLog(@"%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[sourceURL path]]]);
NSURL *newVideoUrl ; //一般.mp4
NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;//这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。我建议删除掉,免得占空间。
[picker dismissViewControllerAnimated:YES completion:nil];
[self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];
}
}
//此方法可以获取文件的大小,返回的是单位是KB。
- (CGFloat) getFileSize:(NSString *)path
{
NSLog(@"%@",path);
NSFileManager *fileManager = [NSFileManager defaultManager];
float filesize = -1.0;
if ([fileManager fileExistsAtPath:path]) {
NSDictionary *fileDic = [fileManager attributesOfItemAtPath:path error:nil];//获取文件的属性
unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue];
filesize = 1.0*size/1024;
}else{
DLog(@"找不到文件");
}
return filesize;
}
//此方法可以获取视频文件的时长。
- (CGFloat) getVideoLength:(NSURL *)URL
{
AVURLAsset *avUrl = [AVURLAsset assetWithURL:URL];
CMTime time = [avUrl duration];
int second = ceil(time.value/time.timescale);
return second;
}
- (void) convertVideoQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
completeHandler:(void (^)(AVAssetExportSession*))handler
{
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
// NSLog(resultPath);
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
NSLog(@"%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:outputURL]]);
NSLog(@"%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[outputURL path]]]);
//UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册
[self alertUploadVideo:outputURL];
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
}
}];
}
-(void)alertUploadVideo:(NSURL*)URL{
CGFloat size = [self getFileSize:[URL path]];
NSString *message;
NSString *sizeString;
CGFloat sizemb= size/1024;
if(size<=1024){
sizeString = [NSString stringWithFormat:@"%.2fKB",size];
}else{
sizeString = [NSString stringWithFormat:@"%.2fMB",sizemb];
}
if(sizemb<2){
[self uploadVideo:URL];
}
else if(sizemb<=5){
message = [NSString stringWithFormat:@"视频%@,大于2MB会有点慢,确定上传吗?", sizeString];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSFileManager defaultManager] removeItemAtPath:[URL path] error:nil];//取消之后就删除,以免占用手机硬盘空间(沙盒)
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self uploadVideo:URL];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}/*else if(sizemb>5){
message = [NSString stringWithFormat:@"视频%@,超过5MB,不能上传,抱歉。", sizeString];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSFileManager defaultManager] removeItemAtPath:[URL path] error:nil];//取消之后就删除,以免占用手机硬盘空间
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
*/
}
-(void)uploadVideo:(NSURL*)URL{
NSData *data = [NSData dataWithContentsOfURL:URL];
[SVProgressHUD showWithStatus:@"上传视频中"];
[Manage requestToUploadAttachmentsDataWith:^(NSDictionary *base) {
if ([base[@"errno"] intValue] == 200) {
[SVProgressHUD showInfoWithStatus:[base objectForKey:@"err"]];
}else{
[SVProgressHUD showInfoWithStatus:[base objectForKey:@"err"]];
}
} taskId:self.taskId fileImg:nil URL:URL enclosureType:self.taskId fileVideo:data];
}
IOS 上传视频并压缩MP4格式
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 端午节快到了,提前祝大家节日快乐。担心端午没时间写,今天抽空写一下。 最近做了一个项目,我把其中的核心功能拿出来和...