0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

use AFNetworking to upload image

Last updated at Posted at 2020-11-24
+(void) commonUploadImageWithUrlStr:(NSString*) urlStr
                              param:(NSDictionary*) param
                              image:(UIImage*) image
                           fileName:(NSString*) fileName
                            success:(void (^)(NSDictionary *))successBlock
                            failure:(void (^)(NSError *))failureBlock{
    if(param != nil) {
        NSData *imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
        
        if(imageData != nil) {
            AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
            NSArray *responseSerializers =@[[AFJSONResponseSerializer serializer]];
            
            manager.responseSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:responseSerializers];
            
            NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"
                                                                                           URLString:urlStr
                                                                                          parameters:param
                                                                           constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                               [formData appendPartWithFileData:imageData
                                                                                                           name:@"image"
                                                                                                       fileName:fileName
                                                                                                       mimeType:@"image/png"];
                                                                           }
                                                                                               error:NULL];
            
            
            AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request
                                                                                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                                     NSInteger statusCode = operation.response.statusCode;
                                                                                     
                                                                                     if(statusCode != 201) {
                                                                                         NSLog(@"ERROR statusCode = %ld",(long)statusCode);
                                                                                         if (failureBlock) failureBlock([NSError errorWithDomain:ServerErroDomain code:statusCode userInfo:nil]);
                                                                                     } else {
                                                                                         NSLog(@"%@",responseObject);
                                                                                         if (successBlock) successBlock(responseObject);
                                                                                     }
                                                                                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                                     NSLog(@"%@",error);
                                                                                     if (failureBlock) failureBlock(error);
                                                                                 }];
            
            // データを送信する度に実行される処理を設定する
            [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                // アップロード中の進捗状況をコンソールに表示する
                NSLog(@"bytesWritten: %@, totalBytesWritten: %@, totalBytesExpectedToWrite: %@, progress: %@",
                      @(bytesWritten),
                      @(totalBytesWritten),
                      @(totalBytesExpectedToWrite),
                      @((float)totalBytesWritten / totalBytesExpectedToWrite));
            }];
            
            // アップロードを開始する
            [manager.operationQueue addOperation:operation];
            
        } else {
            if(failureBlock) failureBlock([NSError errorWithDomain:ParamErrorDomain code:9999 userInfo:nil]);
        }
        
    } else {
        if(failureBlock) failureBlock([NSError errorWithDomain:ParamErrorDomain code:9999 userInfo:nil]);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?