gpt4 book ai didi

objective-c - iOS imageWithData 调整大小为 640 x 480

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:18 25 4
gpt4 key购买 nike

我编写了一个 iOS 方法来将选定的图像上传到网络服务器,因此:

NSData *imageData = UIImagePNGRepresentation(imageView.image);
NSString *urlString = @"http://awebserversomewher.com/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@",returnString);

然而,在提交此图片之前,我想调整它的大小,以便更轻松、更快速地上传,谁能告诉我如何做?

谢谢

贾斯汀

最佳答案

这里有一种方法可以让你最终得到你想要的任何大小(不一定保持比例,这是一个优点和一个缺点):

UIImage *UIImageResize(UIImage *image, CGSize targetSize)
{
UIGraphicsBeginImageContext(targetSize);
CGContextRef bitmapContext = UIGraphicsGetCurrentContext();

CGContextScaleCTM(bitmapContext, 1, -1);
CGContextDrawImage(bitmapContext, (CGRect) { .origin.y = -targetSize.height, .size = targetSize }, image.CGImage);

UIImage *results = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return results;
}

示例用法:

UIImage *resizedImage = UIImageResize([UIImage imageNamed:@"NukeBusters.png"], CGSizeMake(125, 130));
imageView.image = resizedImage;

关于objective-c - iOS imageWithData 调整大小为 640 x 480,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11122848/

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