亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS 使用AFN上傳視頻到服務(wù)器示例代碼

 更新時間:2017年04月17日 15:16:09   作者:Mint丶bin  
本篇文章主要介紹了iOS 使用AFN上傳視頻到服務(wù)器示例代碼,具有一定的參考價值,有興趣的可以了解一下。

這里介紹用AFN上傳本地拍攝的視頻到服務(wù)端。

整體思路:拿到視頻資源,先轉(zhuǎn)為mp4,寫進(jìn)沙盒,然后上傳,上傳成功后要記得刪除沙盒中的文件。

壓縮導(dǎo)出視頻

//視頻轉(zhuǎn)換為MP4
//轉(zhuǎn)碼操作...
_hud.mode = MBProgressHUDModeIndeterminate;
_hud.labelText = @"轉(zhuǎn)碼中...";
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:_filePathURL options:nil];
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyyMMddHHmmss"];
_fileName = [NSString stringWithFormat:@"output-%@.mp4",[formater stringFromDate:[NSDate date]]];
_outfilePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", _fileName];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
  MyLog(@"outPath = %@",_outfilePath);
  AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
  exportSession.outputURL = [NSURL fileURLWithPath:_outfilePath];
  exportSession.outputFileType = AVFileTypeMPEG4;
  [exportSession exportAsynchronouslyWithCompletionHandler:^{
    if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
      MyLog(@"AVAssetExportSessionStatusCompleted---轉(zhuǎn)換成功");
      _filePath = _outfilePath;
      _filePathURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",_outfilePath]];
       MyLog(@"轉(zhuǎn)換完成_filePath = %@\\n_filePathURL = %@",_filePath,_filePathURL);
      //獲取大小和長度
      [self SetViewText];
      [self uploadNetWorkWithParam:@{@"contenttype":@"application/octet-stream",@"discription":description}];
    }else{
      MyLog(@"轉(zhuǎn)換失敗,值為:%li,可能的原因:%@",(long)[exportSession status],[[exportSession error] localizedDescription]);
      [_hud hide:YES];
      [MyHelper showAlertWith:nil txt:@"轉(zhuǎn)換失敗,請重試"];
    }
  }];
}

開始上傳

- (void)uploadNetWorkWithParam:(NSDictionary*)dict {
  MyLog(@"開始上傳_filePath = %@\\n_filePathURL = %@",_filePath,_filePathURL);
  AFHTTPRequestSerializer *ser=[[AFHTTPRequestSerializer alloc]init];
  NSMutableURLRequest *request =
  [ser multipartFormRequestWithMethod:@"POST"
               URLString:[NSString stringWithFormat:@"%@%@",kBaseUrl,kVideoUploadUrl]
               parameters:@{@"path":@"show",@"key":_key,@"discription":dict[@"discription"],@"isimage":@(_isImage)}
       constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
           [formData appendPartWithFileURL:_filePathURL name:@"file" fileName:_fileName mimeType:dict[@"contenttype"] error:nil];
         if (!_isImage) {
    [formData appendPartWithFileURL:_path2Url name:@"tmp" fileName:@"tmp.PNG" mimeType:@"image/png" error:nil];
         }
       } error:nil];
  //@"image/png"  @"application/octet-stream" mimeType
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  NSProgress *progress = nil;
  NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
       MyLog(@"request = %@", request );
       MyLog(@"response = %@", response );
      MyLog(@"Error: %@", error );
      [_hud hide:YES];
      CXAlertView *alert=[[CXAlertView alloc]initWithTitle:NSLocalizedString(@"Warning", nil)
                             message:NSLocalizedString(@"Upload Failed",nil)
                        cancelButtonTitle:NSLocalizedString(@"Iknow", nil)];
      alert.showBlurBackground = NO;
      [alert show];
    } else {
      MyLog(@"%@ %@", response, responseObject);
      NSDictionary *backDict=(NSDictionary *)responseObject;
      if ([backDict[@"success"] boolValue] != NO) {
        _hud.labelText = NSLocalizedString(@"Updating", nil);
        [self UpdateResxDateWithDict:backDict discription:dict[@"discription"]];
        [_hud hide:YES];
      }else{
        [_hud hide:YES];
        [MyHelper showAlertWith:nil txt:backDict[@"msg"]];
      }
    }
    [progress removeObserver:self
           forKeyPath:@"fractionCompleted"
             context:NULL];
  }];
  [progress addObserver:self
        forKeyPath:@"fractionCompleted"
         options:NSKeyValueObservingOptionNew
         context:NULL];
  [progress setUserInfoObject:@"someThing" forKey:@"Y.X."];
  [uploadTask resume];
}

清除documents中視頻

-(void)ClearMovieFromDoucments{
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
  NSEnumerator *e = [contents objectEnumerator];
  NSString *filename;
  while ((filename = [e nextObject])) {
    MyLog(@"%@",filename);
    if ([filename isEqualToString:@"tmp.PNG"]) {
      MyLog(@"刪除%@",filename);
      [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
      continue;
    }
    if ([[[filename pathExtension] lowercaseString] isEqualToString:@"mp4"]||
      [[[filename pathExtension] lowercaseString] isEqualToString:@"mov"]||
      [[[filename pathExtension] lowercaseString] isEqualToString:@"png"]) {
      MyLog(@"刪除%@",filename);
      [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS第三方框架二維碼生成與掃描

    iOS第三方框架二維碼生成與掃描

    這篇文章主要為大家詳細(xì)介紹了iOS第三方框架二維碼生成與掃描,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼

    iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼

    這篇文章主要介紹了 iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼,需要的朋友可以參考下
    2017-01-01
  • iOS實現(xiàn)圖片輪播器

    iOS實現(xiàn)圖片輪播器

    這篇文章主要為大家詳細(xì)介紹了iOS如何實現(xiàn)圖片輪播器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • IOS textField限制字節(jié)長度

    IOS textField限制字節(jié)長度

    這篇文章主要介紹了IOS textField限制字節(jié)長度的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • iOS貝塞爾曲線畫哆啦A夢的代碼實例

    iOS貝塞爾曲線畫哆啦A夢的代碼實例

    本篇文章主要介紹了iOS貝塞爾曲線畫哆啦A夢的代碼實例,這里整理了詳細(xì)的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-07-07
  • 深入了解iOS開發(fā)中UIWindow的相關(guān)使用

    深入了解iOS開發(fā)中UIWindow的相關(guān)使用

    這篇文章主要介紹了深入了解iOS開發(fā)中UIWindow的相關(guān)使用,以及iOS8以后產(chǎn)生的相關(guān)變化,需要的朋友可以參考下
    2015-10-10
  • iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)詳解

    iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)詳解

    關(guān)于應(yīng)用角標(biāo)相信大家應(yīng)該都有所了解吧,這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • iOS Runntime 動態(tài)添加類方法并調(diào)用-class_addMethod

    iOS Runntime 動態(tài)添加類方法并調(diào)用-class_addMethod

    這篇文章主要介紹了iOS Runntime 動態(tài)添加類方法并調(diào)用-class_addMethod的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • iOS實現(xiàn)微信分享多張圖片功能

    iOS實現(xiàn)微信分享多張圖片功能

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)微信分享多張圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • iOS實現(xiàn)一個簡易日歷代碼

    iOS實現(xiàn)一個簡易日歷代碼

    本篇文章主要介紹了iOS實現(xiàn)一個簡易日歷代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03

最新評論