AFNetworking 上傳圖片時超時的解決方案

? ```??

?NSData*imageData;

? ? NSString*mimetype;

? ? //判斷下圖片是什么格式

? ? if (UIImagePNGRepresentation(image) != nil) {

? ? ? ? mimetype =@"image/png";

? ? ? ? imageData =UIImagePNGRepresentation(image);

? ? }else{

? ? ? ? mimetype =@"image/jpeg";

? ? ? ? imageData =UIImageJPEGRepresentation(image,1.0);

? ? }

? ? AFHTTPSessionManager *session = [AFHTTPSessionManager manager];

? ? NSString* urlString = [self uploadUserImageUrl];


? ? NSDictionary* dict =@{@"id":Id};

? ? [session POST:urlString parameters:dict constructingBodyWithBlock:^(id? ?formData) {

? ? ? ? NSString*str =@"avatar";


? ? ? ? NSString * fileName = [[NSString alloc]init];


? ? ? ? if (UIImagePNGRepresentation(image) != nil) {

? ? ? ? ? ? fileName = [NSString stringWithFormat:@"%@.png", str];

? ? ? ? }else{

? ? ? ? ? ? fileName = [NSString stringWithFormat:@"%@.jpg", str];

? ? ? ? }

? ? ? ? // 上傳圖片,以文件流的格式

? ? ? ? /**

?? ? ? ? *filedata : 圖片的data

?? ? ? ? *name? ? : 后臺提供的字段

?? ? ? ? *mimeType : 類型

?? ? ? ? */

? ? ? ? NSString * imagePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];


? ? ? imagePath = [image pathstringByAppendingPathComponent:fileName];

//注意此處一定要用圖片的本地全路徑,不能只是傳文件名

? ? ? ? [formData appendPartWithFileData:imageDataname:@"file" fileName:imagePath mimeType:mimetype];

? ? }progress:^(NSProgress*_uploadProgress) {


? ? }success:^(NSURLSessionDataTask* task, id? responseObject) {


? ? ? ? [self requestSuccessTipWithObject:responseObject];

? ? }failure:^(NSURLSessionDataTask*task,NSError*error) {


? ? ? ? NSLog(@"error = %@",error);

? ? ? ? //請求發(fā)生錯誤回調(diào)


? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{


? ? ? ? ? ?[self requestFailureWithError:error delegate:delegate];


? ? ? ? });

? ? }];?

?```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容