2016-06-09 21 views
0

其實我有JSON參數後的圖像使用多到URL

[dictionary setObject:_dateOfBirth.text forKey:@"birth_date"]; 
    [dictionary setObject:_tfCountry.text forKey:@"country"]; 
    [dictionary setObject:_tfEmail.text forKey:@"email"]; 
    [dictionary setObject:@"" forKey:@"fromLogin"]; 
    [dictionary setObject:@1 forKey:@"gender"]; 
    [dictionary setObject:@"signup" forKey:@"methodName"]; 
    [dictionary setObject:_tfContact.text forKey:@"mobile"]; 
    [dictionary setObject:_tfName.text forKey:@"name"]; 
    [dictionary setObject:_tfNickName.text forKey:@"nickname"]; 
    [dictionary setObject:_tfPassword.text forKey:@"password"]; 
    [dictionary setObject:_tfPinCode.text forKey:@"pincode"]; 

也有一個形象,我必須設置profile_pic爲重點。 現在我已經轉換了所有的參數作爲數據和POST數據躺在這

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:nil]; 

    // this is your service request url 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://proteen2.inexture.com/webservice"]]; 

    // set the content as format 

    [request setHTTPMethod:@"POST"]; 

    [request setHTTPBody: jsonData]; 

    // this is your response type 

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

    NSError *err; 
    NSURLResponse *response; 

    // send the synchronous connection 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

    // here add your server response NSJSONSerialization 

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

它的作品爲文本精細,現在怎麼圖片附加到參數,我只知道用多聲部,但沒有得到該點。請幫助

+0

檢查:http://stackoverflow.com/questions/8042360/nsdata-and-uploading-images-via-post-in-ios –

+0

什麼是邊界?如果我將圖像轉換成NSdata如何將該數據添加到該參數引用到特定鍵。我很困惑 –

回答

0

這裏需要管理很多事情,比如集合boundry追加圖像數據等等等等

您應該使用AFNetworking做是很簡單的。從GitHub,只是拖動下載和刪除庫項目,並導入你的類AFNetworking.h,然後你可以這樣做,

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"urlstring" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 

    //Append image here for example; 

    UIImage *img = tempImageView.image; 
    NSData *imgData = UIImageJPEGRepresentation(img, 0.5); 

    [formData appendPartWithFileData:imgData name:@"imagename/serversideparameter" fileName:@"imagename" mimeType:@"image/jpeg"]; 


} error:nil]; 

//Send this request to server. Something like, 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 

AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; 

[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 

    if (!error) { 

     NSLog(@"success!!"); 

     NSLog(@"here is the response : %@",responseObject); 
    } 
    else{ 

     NSLog(@"Error Occured : %@",error.localizedDescription); 
    } 

}]resume]; 

你不應該使用NSUrlConnection,因爲它現在已經過時。最好使用NSUrlSession,我通過AFNetworking回答。

如果您不想使用AFNetworking則請參考this stackoverflow post。回答中有一個很好的解釋。