查看我用于在Web服务器中发布图像的代码。我可以发送小尺寸的图像。但它不适用于大尺寸图像。我应该修改此代码以启用大尺寸图像
NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@&AgeRange=%@",UserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,gender,imagename,ageTextfield.text];
NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/InsertFacialImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImagePNGRepresentation(chosenImage);
// NSData *data = [NSData dataWithData:UIImagePNGRepresentation(chosenImage)];
[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq)
{
NSLog(@"Connection Sucessful");
}
else {
NSLog(@"failed");
}
[connReq start];
答案 0 :(得分:1)
CGFloat compr = 0.9f; //comress the size of the image
CGFloat maxCompre = 0.1f; //max allowed compression from 0 to 1
int maxSize = 250*1024; //max file size
NSData *imageData = UIImageJPEGRepresentation(yourImage, compr);
while ([imageData length] > maxSize && compr > maxCompre)
{
compr -= 0.1;
imageData = UIImageJPEGRepresentation(yourImage, compression); //compress the size
}
最后你将imageData
传递给服务器