在我的应用中,需要将多个图像上传到服务器。当我们将图像上传到服务器一个或一个所有图像时。
基本问题是。
这是将图像上传到服务器的代码。 使用简单的AFNetwortking。
-(void) uploadImageToServerOneByOne:(int)count andImage:(UIImage*)img {
NSMutableDictionary *parem_dict;
if (parem_dict == nil) {
parem_dict = [[NSMutableDictionary alloc]init];
}
[parem_dict setObject:@"jpeg" forKey:@"extension"];
[parem_dict setObject:@706 forKey:@"order_id"];
[parem_dict setObject:@"gallary" forKey:@"image_type"];
[parem_dict setObject:@(count+1) forKey:@"count"];
[parem_dict setObject:@(1) forKey:@"product_id"];
[parem_dict setObject:@"SNAPBOOK" forKey:@"type"];
manager.requestSerializer.timeoutInterval = 1200 ;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[manager POST:@"http://52.42.247.73:8080/anaventures/image/upload" parameters:parem_dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(img, 0.5) name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
[parem_dict removeAllObjects];
formData = nil;
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%f",uploadProgress.fractionCompleted);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[MyLoader hideLoadingView];
if (count + 1 < imageArray.count) {
[self uploadImageToServerOneByOne:count +1 andImage:imageArray[count+1]];
}else{
[MyLoader hideLoadingView];
NSLog(@" image successfully uploaded -------- ohhhhhhhhooooooooooo!");
}
NSLog(@"success");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[MyLoader hideLoadingView];
NSLog(@"fail%@", error.localizedDescription);
}];
}
提前致谢
答案 0 :(得分:0)
您的问题是您没有使用异步方法上传图片。
我向您展示了一个上传图片方法和示例:
Util method:
+ (void)uploadImage:(UIImage *)image withUrl:(NSString *)uploadUrl withBlock:(void (^)(NSString *))returnUrl {
UIImage *img = image;
//UIImage *com_img = [Util compressImage:img toMaxFileSize:25 * 1000]; // max is 250kb // 50 *1000 :333759
UIImage *com_img_before = [Util imageCompressForWidth:img targetWidth:80];
UIImage *com_img = [Util compressImage:com_img_before toMaxFileSize:25 * 1000];
//NSData *dataObj = UIImagePNGRepresentation(com_img);
NSData *dataObj;
if (UIImagePNGRepresentation(com_img) == nil) {
dataObj = UIImageJPEGRepresentation(com_img, 1);
} else {
dataObj = UIImagePNGRepresentation(com_img);
}
NSDictionary * param1 = @{
@"id":@1,
@"imgFile":dataObj
};
AFHTTPSessionManager * session = [AFHTTPSessionManager manager];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"application/xml",@"text/json",@"text/javascript",@"text/html",@"text/plain",@"multipart/form-data",nil];
//url
NSString *url = [NSString stringWithFormat:@"%@%@", BASE_URL, uploadUrl]; // uploadHeader uploadImages
[session POST:url parameters:param1 constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
UIImage *img = image; // response
NSData *data = UIImagePNGRepresentation(img);
// schoolListItem@2x
[formData appendPartWithFileData:data name:@"imgFile" fileName:@"imgFile.png"mimeType:@"image/png"];
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"success+%@", responseObject);
NSString *return_url = [NSString stringWithFormat:@"%@",responseObject[@"data"]]; //
// pass data by block
returnUrl(return_url);
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
NSLog(@"fail");
}];
}
USAGE:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
__block int comemntCount = 0;
NSString *url = @"prescription/uploadImages"; // the uplad url
static dispatch_once_t onceToken;
static dispatch_semaphore_t semaphore; // sema
static dispatch_queue_t queue;
dispatch_once(&onceToken, ^{
semaphore = dispatch_semaphore_create(1);
//queue = dispatch_queue_create("com.MyApp.task", NULL);
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
});
for (int i = 0; i < _selectedAssets.count; i ++) { //_photosChooseArr
dispatch_async(queue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[Util uploadImage:_selectedPhotos[i] withUrl:url withBlock:^(NSString *returnUrl) {
if (i == 0) {
files = returnUrl;
comemntCount ++;
if (comemntCount == _selectedAssets.count) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self doUploadMessageWithFiles:files];
}
}else {
files = [files stringByAppendingString:[NSString stringWithFormat:@",%@", returnUrl]];
comemntCount ++;
if (comemntCount == _selectedAssets.count) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self doUploadMessageWithFiles:files];
NSLog(@"i = %d, returnUrl = %@, files = %@", i, returnUrl, files);
}
}
dispatch_semaphore_signal(semaphore);
}];
});
}
/* 3 min after hide the hud */
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(180.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 60s after close the hud
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
这是我上传图像数组的方法,你可以复制到你的项目进行研究,希望对你有帮助。