iOS uploads voice, text, pictures

Source: Internet
Author: User

需求:用户可能提交 文字,也可能有语音,还有可能有图片。当然 都有也是可以的。在这里我的方案是 先做判断(因为我提交的类不同(也就是方法不一样));
    (1)if (_imageArray.count00){  //只有文字}else{    // 文字 图片 加录音}

Method:

// 有文字,有图,片有语音+(void)askTextImageByUserInfoId:(NSString *)userInfoId                WithUserRelationId:(NSString *)userRelationId                 WithAskContent:(NSString *)askContent             WithImageDataArray:(NSMutableArray *)imageDataArray                  WithVoiceData:(NSData *)voiceData                        success:(void (^)(id askDescArray))success                        failure:(void (^)(NSError *error))failure`

Specific implementation:

/** * Request Opinion Server Api interface method * * @param requests Customized request requests * @param userinfoid Unique identifier of the corresponding person (user and related person) * @par  AM Token token value * @param imageandvoicedataarray an array of voice images * @param success request succeeded, return response object * @param failure request failed * @param cached whether to cache * /+ (void) Requestwithpost: (networkbaserequest *) Request Withimagedataarray: (Nsmutablearray*) Imagedataarray Withvoicedata: (NSData *) VoiceData success: (void(^) (Networkbaseresponse *responseobject)) Success Failure: (void(^) (Nserror*ERROR)) Failure cache: (BOOL) cached{if(!request) {NSLog(@"[ERROR] request cannot be NULL");if(failure) {Failure ([Nserrorerrorwithdomain:@""code:-1UserInfo:Nil]); }return;    } Afhttpsessionmanager *manager = [Afhttpsessionmanager manager]; [Manager. Requestserializersetvalue:@"Multipart/form-data;charset=utf-8"forhttpheaderfield:@"Content-type"]; Manager. Requestserializer. timeOutInterval=Ten; Manager. Responseserializer= [Afjsonresponseserializer serializer]; [Manager. RequestserializerSetcachepolicy:nsurlrequestreturncachedataelseload];NSString*tokenvalue = [Sskeychain passwordforservice:[apputils appservicename] account:[apputils TokenKey]];if(!tokenvalue) {Tokenvalue = @""; }Else{[Request. ParamdicSetvalue:tokenvalue forkey:@"token"]; } [Manager post:request. stringofapiurlstringParameters:request. Paramdicconstructingbodywithblock:^ (ID<AFMultipartFormData> _nonnull formData) {//Iterate through the array of images passed in         for(inti =0; I < Imagedataarray. Count; i++) {NSData *imgdata = imagedataarray[i];//Use this as the name of the picture, together to the effect of the picture name not repeatingNSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];NSString*tupianming = [NSStringstringwithformat:@"yyyymmddhhmmssssss%d", I]; [Dateformatter setdateformat:tupianming];NSString*pictname;NSString*picfilename; Pictname = [NSStringstringwithformat:@"/%@.jpg", [Dateformatter stringfromdate:[NSDateDate]]; Picfilename = [NSStringstringwithformat:@"/% @file", [Dateformatter stringfromdate:[NSDateDate]]; [FormData appendpartwithfiledata:imgdata Name:picfilename filename:[NSStringstringwithformat:@"%@", Pictname] mimetype:@"Image/jpg"]; }if(voicedata!=Nil){//Use this as the name of the picture, the effect of the image name does not repeatNSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];NSString*tupianming = [NSStringstringwithformat:@"yyyymmddhhmmssssss%@",@"Luyin"]; [Dateformatter setdateformat:tupianming];NSString*luyinname;NSString*luyinfilename; Luyinname = [NSStringstringwithformat:@"/%@.amr", [Dateformatter stringfromdate:[NSDateDate]];//Let the server recognize this audio format for AMRLuyinfilename = [NSStringstringwithformat:@"/%@.amr", [Dateformatter stringfromdate:[NSDateDate]]; [FormData appendpartwithfiledata:voicedata name:luyinname filename:luyinfilename mimetype:@"Audio/amr"]; }} Progress:Nilsuccess:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable responseobject) {nsdictionary*dic = Responseobject;//operation.request.cachepolicy        if(DIC) {@try{//Resolve if JSON format error crashes, Judge Status=0 as successful                NSString*responseclassname = [Networkutil replacecharacter:@"Request"withstring:@"Response"Instring:nsstringfromclass ([request class]); Networkbaseresponse *response = [[Nsclassfromstring (Responseclassname) alloc] initwithdictionary:dic];if(Response && Success)                {Success (response); }Else{Failure ([Nserrorerrorwithdomain:@"Interface Error"code:-1UserInfo:Nil]); }            }@catch(NSException*exception) {failure ([Nserrorerrorwithdomain:@"interface returned data format error"code:-1UserInfo:Nil]); }        }Else if(failure) {Failure ([Nserrorerrorwithdomain:@"Interface Error"code:-1UserInfo:Nil]); }Else{NSLog(@"[ERROR] Failure not exist!"); }} failure:^ (Nsurlsessiondatatask * _nullable task,Nserror* _nonnull error) {failure (error); }];}

iOS uploads voice, text, pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.