Ios AF3.0上传图像

Ios AF3.0上传图像,ios,afnetworking,Ios,Afnetworking,我正在尝试使用AFNetworking 3.0将图像上传到我的服务器。我的服务器返回“请选择要上载的文件”。下面是如何在我的php文件$filename=$\u FILES[“file”][“name”]中捕获上传的文件 -(void)uplodeImages :(NSString *)image { NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithM

我正在尝试使用AFNetworking 3.0将图像上传到我的服务器。我的服务器返回“请选择要上载的文件”。下面是如何在我的php文件
$filename=$\u FILES[“file”][“name”]中捕获上传的文件

-(void)uplodeImages :(NSString *)image {
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://local/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:image] name:@"file" fileName:@"imagename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

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

    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                          NSLog(@"Laddar...");
                      });
                  } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      if (error) {
                          NSLog(@"Error: %@", error);
                      } else {
                          NSLog(@"%@ %@", response, responseObject);
                      }
                  }];
    [uploadTask resume];
}
-(void)上传图像:(NSString*)图像{
NSMutableURLRequest*请求=[[AFHTTPRequestSerializer]multipartFormRequestWithMethod:@“POST”URLString:@”http://local/upload.php“参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileURL:[NSURL fileURLWithPath:image]名称:@“文件”文件名:@“imagename.jpg”mimeType:@“image/jpeg”错误:nil];
}错误:无];
AFURLSessionManager*manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask*上传任务;
uploadTask=[manager uploadTaskWithStreamedRequest:请求进度:^(NSProgress*\u非空uploadProgress){
dispatch\u async(dispatch\u get\u main\u queue()^{
NSLog(@“Laddar…”);
});
}completionHandler:^(NSURLResponse*\u非空响应,id\u可空响应对象,NSError*\u可空错误){
如果(错误){
NSLog(@“错误:%@”,错误);
}否则{
NSLog(@“%@%@”,响应,响应对象);
}
}];
[上传任务恢复];
}

注意:-我刚刚使用AFNetworking 3.0实现了图像上传服务

->这里kBaseURL表示服务器的基本URL

->serviceName是指服务的名称

->dictParams表示在需要时提供参数,否则为零

->图像意味着传递你的图像

注意:-这段代码是用NSObject类编写的,我们已经应用到我们的项目中

+ (void)requestPostUrlWithImage: (NSString *)serviceName parameters:(NSDictionary *)dictParams image:(UIImage *)image success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {

NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[SVProgressHUD show];

NSData *fileData = image?UIImageJPEGRepresentation(image, 0.5):nil;

NSError *error;
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:strService parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    if(fileData){
        [formData appendPartWithFileData:fileData
                                    name:@"image"
                                fileName:@"img.jpeg"
                                mimeType:@"multipart/form-data"];
    }
} error:&error];

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

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

    NSLog(@"Wrote %f", uploadProgress.fractionCompleted);
}
                                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                      [SVProgressHUD dismiss];
                                      if (error)
                                      {
                                          failure(error);
                                      }
                                      else
                                      {
                                          NSLog(@"POST Response  : %@",responseObject);
                                          success(responseObject);

                                      }
                                  }];

[uploadTask resume];

}
UIImage *imgProfile = //Pass your image.        


    [WebService requestPostUrlWithImage:@"save-family-member" parameters:nil image:imgProfile success:^(NSDictionary *responce) {

        NSString  *check = [NSString stringWithFormat:@"%@",[responce objectForKey:@"status"]];
        if ([check  isEqualToString: @"1"]) {
           //Image Uploaded. 
        }
        else
        {
         //Failed to Upload.
        }


    } failure:^(NSError *error) {
        //Error
    }];