iOS 發佈圖片到服務器 AFNetworking

+ (void)getAddMedicineWithDic:(NSDictionary *)dic object:(CompletionType)type withImageArr:(NSArray *)imageArr{app

//    [self networkRequestUrlString:[DIEToolkit addMedicine] paramsDic:dic object:type];async

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[DIEToolkit addMedicine] parameters:[self params:dic] constructingBodyWithBlock:^(id<AFMultipartFormData_Nonnull formData) {spa

        for (UIImage *image in imageArr) {3d

            NSData *data = UIImageJPEGRepresentation(image, 1);orm

            [formData appendPartWithFileData:data name:@"upload" fileName:@"filenanme.jpg" mimeType:@"image/jpeg"];ip

        }ci

    } error:nil];get

    [self afUrlSessionManagerWithUrlRequest:request object:type];it

}io

+ (void)afUrlSessionManagerWithUrlRequest:(NSMutableURLRequest *)request object:(CompletionType)completion

{

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    

    NSURLSessionUploadTask *uploadTask;

    uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * __autoreleasing _Nonnull uploadProgress) {

                                            dispatch_async(dispatch_get_main_queue(), ^{

                        });

                  }

                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

                      if (error) {

                          NSLog(@"Error: %@", error);

                          completion(nil,error);

                      } else {

                          NSLog(@"%@ %@", response, responseObject);

                          completion(responseObject,nil);

                      }

                  }];

    [uploadTask resume];

}

相關文章
相關標籤/搜索