Background upload of large number of images using AFNetworking - ios

I am looking for uploading about 100 images in the background using AFNetworking ,do I need to create an operations queue? or I can create multiple request in a for loop like so:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = nil;
[manager.requestSerializer setTimeoutInterval:30.0];
for(MyImageView *myImageView in images){
NSDictionary *parameters = _PARAMETERS;
[manager POST:#"//MY_URL_" parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(myImageView.image, 0.65) name:#"NAME" fileName:#"name" mimeType:#"image/jpeg"];
}progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionTask *task, id responseObject) {
NSDictionary *response = (NSDictionary *)responseObject;
dispatch_async(dispatch_get_main_queue(), ^{
int success = [[response objectForKey:#"success"] intValue];
if(success == 1) {
NSLog(#"IMAGE UPLOAD SUCCESSFULL");
}
else if (success == 0){
NSLog(#"IMAGE UPLOAD FAILED");
}
NSLog(#"%#",response);
});
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"%#",error.localizedDescription);
}];
}
How do I create an NSOperations Queue if need be?
Thanks

Use AFHTTPRequestOperation and NSOperarationQueue with NSOperationQueueDefaultMaxConcurrentOperationCount

Related

Unable to save picture in SignUp POST request with Afnetworking

I am trying to upload a picture on server with Multiparts using AFNetworking. I have tried to make a simple POST request without Image and it works fine. Now, can say that service URL is absolutely fine and there is no issue with server and I can see that image URL that is saved in document directory is also fine and all the other parameters are fine too, because they all are working with simple request. Can anyone find some error in my code? My Code is:
(void)uploadPicture:(NSMutableDictionary *)param
{
NSString *string=[NSString stringWithFormat:#"%#%#",base_url,#"register"];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:#"POST" URLString:string parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:getImagePath] name:#"picture" fileName:getImagePath mimeType:#"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(#"Error: %#", error);
} else {
NSLog(#"%# %#", response, responseObject);
NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dic=responseObject;
NSLog(#"");
}
}];
[uploadTask resume];
}
Try this function :
First Solution
Note : You need to customise as per your need
-(void)getResponeseWithURL:(NSString *)url WithParameter:(NSDictionary *)parameter WithImage:(UIImage *)image ImageArray:(NSMutableArray *)arrImage WithImageParameterName:(NSString *)imagename WithCallback:(void(^)(BOOL success, id responseObject))callback {
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:#"POST" URLString:[NSString stringWithFormat:#"%#%#",BASEURL,url] parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (image) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.8) name:imagename fileName:#"Image.jpg" mimeType:#"image/jpeg"];
}
else if (arrImage){
int i = 1;
for (UIImage *recipeimage in arrImage) {
// this condition for maintain server side coloum format : ex name , name_2 , name_3
[formData appendPartWithFileData:UIImageJPEGRepresentation(recipeimage, 0.8) name:i == 1 ? imagename : [NSString stringWithFormat:#"%#_%d",imagename,i] fileName:#"Image.jpg" mimeType:#"image/jpeg"];
i++;
}
}
}error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
[UtilityClass showAlertWithMessage: #"Please try again" andWithTitle:#"Network Error" WithAlertStyle:AFAlertStyleFailure];
NSLog(#"Error: %#", [[NSString alloc]initWithData:[[error valueForKey:#"userInfo"] valueForKey:#"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
[UtilityClass removeActivityIndicator];
callback(NO,nil);
} else {
callback(YES,responseObject);
}
}];
[uploadTask resume];
}
Second
May you have forgot to add ATS in your project plist file so you need to add this .
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSString *string=[NSString stringWithFormat:#"%#%#",base_url,#"register"];
#autoreleasepool {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:strurl
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSMutableArray *allKeys = [[imgParameters allKeys] mutableCopy];
for (NSString *key in allKeys) {
id object = [imgParameters objectForKey:key];
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *str = [[NSString alloc] initWithFormat:#"%d", timestamp];
NSString *ranstrin = [self randomStringWithLength:8];
// if ([key isEqualToString:#"image"]) {
str = [NSString stringWithFormat:#"TestThumb_%d_%#.jpg",
timestamp, ranstrin];
[formData appendPartWithFileData:object
name:key
fileName:str
mimeType:#"image/jpeg"];
}
}
progress:^(NSProgress *_Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask *_Nonnull task,
id _Nullable responseObject) {
complete(responseObject, nil);
}
failure:^(NSURLSessionDataTask *_Nullable task,
NSError *_Nonnull error) {
complete(nil, error);
}];

Upload process blocks other requests AFNetworking 3.0

I am trying to upload data with AFNetworking and its successful. no problem with that but the problem is when I am doing it in background than the other requests blocks and doesn't give any response until upload progress finishes.
What I did is I made a singleton and declare AFURLSessionManager in that because I want all current running task.
+ (id)sharedInstance
{
static SharedManager *sharedMyManager = nil;
#synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
Let me tell you simple example.. for one controller, I made a request for upload process like below
ViewController1.m
-(void)postImage{
#try {
NSMutableDictionary *otherParameters = [[NSMutableDictionary alloc]initWithDictionary:_currentPlaceParameters];
[otherParameters setObject:[UtitlityManager getUserID] forKey:#"user_id"];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:#"POST" URLString:URL_UPLOAD_PHOTO_VIDEO parameters:otherParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *thumbImageData = UIImageJPEGRepresentation(dataValue, 0.5);
[formData appendPartWithFileData:thumbImageData name:#"thumbnail" fileName:#"image.jpg" mimeType:#"image/jpeg"];
} error:nil];
//show progress bar on window
AFURLSessionManager *manager = [[SharedManager sharedInstance]manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
float uploadActualPercentage = uploadProgress.fractionCompleted * 100;
NSLog(#"Multipartdata upload in progress: %#",[NSString stringWithFormat:#"%f %%",uploadActualPercentage]);
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
} else {
NSDictionary *result=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if([[result objectForKey:#"success"] integerValue]==1){
}else{
[Common showMessageWithMessage:#"Failed, Try again"];
}
}
}];
[uploadTask resume];
} #catch (NSException *exception) {
NSLog(#"EXC: %#",[exception description]);
}
}
And than I try to move another view controller and send a request for get other data ...
ViewController2.m
-(void)getData{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
} failure:^(NSURLSessionTask *operation, NSError *error) {
}];
}
But this doesn't give response until progress finishes... why so??
Even I try to set this upload process in dispatch_sync but it won't work ... any suggestion on this???

How to send image as parameters (param) with POST with AFNetworking

I need to send image as param like
URl : some API
params : {profileImage:string(file)}
Means in param list only i have to send image file as string.
i used the below code. but it is not working.
NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(selectedImage);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *params = {profileImage : stringImage}
NSString *url = [NetworkRoutes postProfileImageAPIWithMobileNumber:[PTUserDetails getMobileNumber]];
self.operationManager = [AFHTTPSessionManager manager];
self.operationManager.responseSerializer = [AFJSONResponseSerializer serializer]; //
[self.operationManager.requestSerializer setAuthorizationHeaderFieldWithUsername:#“userName” password:#“some password”];
[self.operationManager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSError *error;
if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:#"file" fileName:[path lastPathComponent] mimeType:#"image/jpg" error:&error]) {
NSLog(#"error appending part: %#", error);
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
your answer no need to be in afnetworking , can also be in nsurlconnection
I am getting resposne
{
response :"Please upload image file"
}
OR
Suggest me how to do like in the attached screen shot . In post man i am getting response
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
NSUInteger fileSize = [imgData length];
if(fileSize>400000)
{
float size = (float)((float)400000/(float)fileSize);
imgData = [NSData dataWithData:UIImageJPEGRepresentation(image, size)];
}
NSString *imgProfilePic = [imgData base64Encoding];
and then you can send this imgProfilePic to Webservice
If you send your image in multipart then this might be helpful and easiest way than BASE64
and also no need to convert your image into BASE64 String.
- (void)uploadImage:(UIImage*)image withParams:(NSDictionary*)paramsDict withURL:(NSString *)URL
{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URL parameters:paramsDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageData!=nil) {
[formData appendPartWithFileData:imageData name:#"imagename" fileName:#"filename" mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"success = %#", responseObject);
[appDelegate dismissLoading];
if ([[responseObject valueForKey:#"code"] isEqualToString:#"200"])
{
// code after success
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[appDelegate dismissLoading];
NSLog(#"error = %#", error);
}];
}
Try to send like following (one of the below) way:
1.
-(void)uploadimage{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://your server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
// if you want to pass another parameter with image then
NSDictionary *param = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *operation = [manager POST:#"rest.of.url" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[operation start];
}
2.
UIImage *image = [UIImage imageNamed:#"imageName.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);
NSString *queryStringss = [NSString stringWithFormat:#"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:#"fileName" fileName:#"imageName.png" mimeType:#"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *dict = [responseObject objectForKey:#"Result"];
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];

Unable to send Multipart-Form data with AFNetworking 3.0

Currently, I've trying to write a method in Objective C that takes in a UIImage and a NSString and makes a POST request to a server. Unfortunately, I have trouble getting it to send anything. When I tried the form with an HTML form, I was able to successfully upload a photo and the parameters. However, I am unable to replicate it in my iOS app. I keep getting a 404 error when I try to make a POST request.
I've tried two different methods of sending a multiform data POST request according to its Github page to no success. I've debugged the UIImage in Xcode and I see that it actually exists. I'm thinking it has something to do with the parameters I'm sending. Should the parameters be part of formDataAppend or just as a dictionary as I did below?
The commented part corresponds with the tutorial on its Github. The uncommented section contains parts from browsing Stackoverflow still to no avail.
+(void) uploadPhotoWithID:(NSString *)ownerID andPhotoImage:(UIImage *)image andCompletionHandler:(void (^)(MZPhoto *, NSError *))callback{
NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSDictionary *parameters = #{#"owner_id": ownerID};
// NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:#"POST" URLString:upload_photo_url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// [formData appendPartWithFileData:imageData name:#"photo" fileName:#"temp.jpg" mimeType:#"image/jpeg"];
// } error:nil];
// AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//
// NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
// if(error){
// NSLog(#"File Upload Failed");
//
// }
// else{
// NSLog(#" %#", responseObject);
// MZPhoto *photo = [[MZPhoto alloc] initWithDictionary:responseObject error:nil];
// callback(photo, nil);
// }
// }];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:upload_photo_url]];
[manager POST:upload_photo_url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:#"photo" fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(#" %#", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(#" Failed");
}];
}
Here is my Node.JS function,
exports.upload_photo = function(req, res, next) {
var owner_id = req.body.owner_id;
console.log(owner_id);
pg.connect(connectionString, function(err, client, done) {
if (err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
var query = client.query("INSERT INTO photos (likes, dislikes, user_id) " +
"VALUES (0, 0, $1) RETURNING photo_id, likes, dislikes, user_id", [owner_id]);
var result;
console.log(req.file);
query.on('row', function(row) {
row.file_url = baseFileURL + row.photo_id + '.jpg';
result = row;
});
query.on('end', function() {
done();
fs.rename('./static/photos/' + req.file.filename, './static/photos/' + result.photo_id + '.jpg', function(err) {
if(err) console.log("Error Renaming the file!");
return res.json(result);
});
});
});
console.log("Received photo upload from: " + owner_id);
}
Try with following function
- (void) postServerRequest:(NSDictionary *)paramDict withData:(NSData*)fileData andType:(uploadType)uploadType Callback:(ResponseBlock) block{
NSString * strServiceURL = #"Your Webservice URL";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:#"multipart/form-data" forHTTPHeaderField:#"Content-Type"];
[manager POST:strServiceURL parameters:paramDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (fileData!=nil){
[formData appendPartWithFileData:fileData name:#"Key name on which you will upload file" fileName:#"Image name" mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
DLOG(#"Success: %#", responseObject);
block(responseObject,nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLOG(#"Error: %#", error);
block (nil , error);
}];
}

Upload image using AFnetworking did not response anything

Here's the problem :
I want to upload image immediately after finish choosing image from imagepicker.
So, I put my code to upload image using afnetworking in imagepicker delegate method.
But it doesn't response anything.
//set the imageview to current image after choosing
profilePic.image = image;
//dismiss the imagepicker
[picker dismissModalViewControllerAnimated:YES];
//start upload image code
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"steventi1901", #"username",
UIImagePNGRepresentation(profilePic.image), #"profile_pic",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:updateProfile];
NSData *imageToUpload = UIImagePNGRepresentation(profilePic.image);
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:#"PUT" path:#"" parameters:params
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:#"file" fileName:#"temp.png" mimeType:#"image/png"];
//NSLog(#"dalem");
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(#"response: [%#]",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if([operation.response statusCode] == 403){
NSLog(#"Upload Failed");
return;
}
NSLog(#"error: %#", [operation error]);
}];
[operation start];
//end upload image code
It really didn't response anything so i didn't know if the process fail or success.
Best and Easy way to upload Image using Afnetworking.
Please use below AFNetworking.
pod 'AFNetworking', '~> 2.5.4'
//Create manager
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//parameters if any
NSMutableDictionary *AddPost = [[NSMutableDictionary alloc]init];
[AddPost setValue:#"Addparma" forKey:#"param"];
NSString * url = [NSString stringWithFormat:#"www.addyourmainurl.com"];;
NSLog(#"AddPost %#",AddPost);
[manager POST:url parameters:[AddPost copy] constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//add image data one by one
for(int i=0; i<[Array count];i++)
{
UIImage *eachImage = [Array objectAtIndex:i];
NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:#"image%d",i] fileName:[NSString stringWithFormat:#"image%d.jpg",i ] mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
have you set the delegate of the picker?
[yourPicker setDelegate:self]

Resources