Reputation: 71
I am creating an app where i have to upload image using MultiPart (File-Type) which I am getting from the gallery. Can anyone tell me how to do it. How would I pass the parameter with that image. I searched a lot but didn't find any solution.
My code is like:-
NSURL *baseUrl=[NSURL URLWithString:baseUrlStringForUserRegisterPage];
NSDictionary *parameters=@{@"pic":_imageViewUserSignUpView.image};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
[manager POST:baseUrlStringForUserRegisterPage parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
}
progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
}];
baseUrlStringForUserRegisterPage carry my URL. I am using AFNetworking here.
Upvotes: 0
Views: 41
Reputation: 14040
this is how it should work:
NSURL *baseUrl = [NSURL URLWithString:baseUrlStringForUserRegisterPage];
// use parameters for other parameters than the images - if needed
// NSDictionary *parameters = @{};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// prepare your image(s)
UIImage *imageToUpload = _imageViewUserSignUpView.image;
NSData *imageData = UIImagePNGRepresentation(imageToUpload);
// NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 0.75) // if you want to upload jpeg instead of png
[manager POST:baseUrlStringForUserRegisterPage parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"pic.png" mimeType:@"image/png"];
// [formData appendPartWithFileData:imageData name:@"pic" fileName:@"pic.png" mimeType:@"image/jpeg"]; // if you want to upload jpeg instead of png
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"failure");
}];
Upvotes: 1