gpt4 book ai didi

ios - AFNetworking 使用 PUT 请求上传图片?

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

我正在尝试通过 PUT 请求使用 AFNetworking 将图像上传到服务器。-

     UIImage* snap = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(snap, 0.3);
NSMutableString * fullPath = [NSMutableString stringWithString:API_BASE_URL];
[fullPath appendFormat:@"%@%@",API_VERSION,req];
NSURL * url = [NSURL URLWithString:fullPath];

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];

[manager.requestSerializer setValue:[NSString stringWithFormat:@"%@", @kPublicKey] forHTTPHeaderField:@"X-API-KEY"];
[manager.requestSerializer setValue:bodyStr forHTTPHeaderField:@"X-API-DATA"];
NSString *URLString = fullPath;

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"media" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
} error:nil];


AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

//success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"failure...");

}];

[requestOperation start];

我正在使用 iPhone 相机拍摄图像并将其上传到服务器,但处理时间太长,上传到服务器的图像尺寸很大(~10-12MB),尽管我正在尝试压缩图像?我做错了什么?任何建议或示例代码将不胜感激。

最佳答案

NSData *imageData = UIImageJPEGRepresentation(snap, 1.0);

UIImageJPEGRepresentation 中的第二个参数表示图像的压缩质量。根据 Apple 文档:

compressionQuality :
The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

尝试将其减少到一个平衡图像质量和上传速度的数字。

关于ios - AFNetworking 使用 PUT 请求上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909054/

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