我有一种将图像上传到服务器的方法 它可以在iOS 8上传图像,但无法在iOS 7上传图像 图像由相机拍摄,我不会调整大小或编辑它。
这是我的代码:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
//Create boundary, it can be anything
NSString *boundary = @"AaB03xChuTieuDaDuyen";
//Set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
//POST body
NSMutableData *body = [NSMutableData data];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[Utilities getTokenMD5] forKey:@"token"];
[parameters setValue:idKH forKey:@"idKH"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *FileParamConstant = @"CHUTIEUIMAGE";
NSData *imageData = UIImageJPEGRepresentation(imageOrinal , 0.5);
UIImage * imagetest = [UIImage imageWithData:imageData];
//Assuming data is not nil we add this to the multipart form
if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
//Close off the request with the boundary
//[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Setting the body of the post to the request
[request setHTTPBody:body];
//Set URL
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[userDefaults objectForKey:@"urlserver"],ServerApiSendImage]]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
// operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/xhtml+xml"];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self stopLoading];
NSLog(@"send Images response obj =%@",responseObject);
[self sendImageRequestDidSuccess : responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self stopLoading];
NSLog(@"error : %@",[error localizedDescription]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
message:@"no connect"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
//Bytes received - saved bytesRead variable to NSUserDefaults
NSLog(@"Camera Send Image Down load = %lld",(long long)totalBytesRead);
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
//Bytes sent - saved bytesWritten variable to NSUserDefaults
NSLog(@"Camera Send Image Up load = %lld",(long long)totalBytesWritten);
}];
[operation start];