ios - 如何使用字典 iOS 在服务器上发布图像?
<p><p>我有一个包含一些键/值的 <code>dictionary (dict)</code> 和一个 <code>image</code>。现在我想用字典将图像上传到服务器。我已经尝试过了,但没有取得任何成功。这是我的示例代码--</p>
<pre><code>NSMutableURLRequest *request = [ init];
];
;
NSMutableData *body = ;
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = ;
;
NSData *imageData = UIImageJPEGRepresentation(image,0.5);//or you can use png representation- UIImagePNGRepresentation(image);
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
;
];
//parameter all_data
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
];
// close form
dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
;
NSError *error=nil;
NSHTTPURLResponse *response=nil;
NSData *urlData=;
</code></pre>
<p>我需要帮助我哪里出错了..提前谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试下面的代码:</p>
<pre><code> -(void)uploadImage:(NSString *)api :(NSDictionary *)params : (UIImage *)image {
NSURL *myURL = ;
NSMutableURLRequest *request = [ initWithURL:myURL];
;
;
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = ;
;
NSMutableData *theBodyData = ;
for (int i=0; i<[ count]; i++)
{
dataUsingEncoding:NSUTF8StringEncoding]];
NSString *value=@"";
value= objectAtIndex:i]];
NSString * value1= objectAtIndex:i],value];
];
}
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
NSData *myData = UIImageJPEGRepresentation(image,0.5);
];
dataUsingEncoding:NSUTF8StringEncoding]];
;
;
] forHTTPHeaderField:@"Content-Length"];
;
NSURLSessionConfiguration *config = ;
NSURLSession *session = ;
NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil) {
NSString *responseString = [ initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Server Raw Response : %@",responseString);
}
}];
;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用字典 iOS 在服务器上发布图像?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36745032/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36745032/
</a>
</p>
页:
[1]