gpt4 book ai didi

ios - HTTP Post 中的大尺寸图像问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:58 25 4
gpt4 key购买 nike

查看我用来在 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];

最佳答案

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传递给服务器

关于ios - HTTP Post 中的大尺寸图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24141137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com