2014-09-21 49 views
0

我想將用戶信息上傳到服務器。它包含用戶頭像和用戶個人資料數據。現在的問題是,用戶不會永遠上傳頭像,所以我想過濾圖像,如果用戶不上傳,所以任何人都可以幫助我嗎?使用Afnetworking發送沒有圖像的數據

現在,我使用此代碼正在上傳圖像,它工作正常,但我想,即使用戶沒有上傳圖片

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]]; 
     NSData *imageData = UIImagePNGRepresentation(image); 
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success %@", responseObject); 
     [Util showAlertDialog: NSLocalizedString(@"Success", nil):NSLocalizedString(@"Upload Successful", nil)]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Failure %@, %@", error, operation.responseString); 
     [ Util showAlertDialog: NSLocalizedString(@"Failed", nil):NSLocalizedString(@"Upload Failed, Please try again", nil)]; 
    }]; 

回答

0

以及我解決它自己發送空的NSData

上傳
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]]; 
     NSData *imageData; 
     CGImageRef cgref = [image CGImage]; 
     CIImage *cim = [image CIImage]; 

     if (cim == nil && cgref == NULL) 
     { 
      imageData = [[NSData alloc] init]; 
     }else{ 
      imageData = UIImagePNGRepresentation(image); 
     } 

     [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success %@", responseObject); 
     [MBProgressHUD hideHUDForView:view animated:YES]; 
     [Util showAlertDialog: NSLocalizedString(@"Success", nil):NSLocalizedString(@"Upload Successful", nil)]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Failure %@, %@", error, operation.responseString); 
     [MBProgressHUD hideHUDForView:view animated:YES]; 
     [ Util showAlertDialog: NSLocalizedString(@"Failed", nil):NSLocalizedString(@"Upload Failed, Please try again", nil)]; 
    }];