gpt4 book ai didi

ios - 在 IOS 中将图像发布到 PHP Web 服务

转载 作者:行者123 更新时间:2023-11-29 00:22:26 25 4
gpt4 key购买 nike

我制作了一些 ImageView 及其下方的按钮。当用户从图库中选择图像时,它会显示在 ImageView 中。现在我希望通过 PHP Web 服务将该图像发送到服务器并使用 POST 方法。我希望它先将其转换为 base64 位。

我的代码是

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ddMMyyyyHHmmss"];

NSData *data = UIImageJPEGRepresentation(_img1.image, 1.0);
UIImage *imagedata = [UIImage imageWithData:data];

NSString * base64String = [data base64EncodedStringWithOptions:0];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];

//---------- Compress photo----------
double compressionRatio = 1;
int resizeAttempts = 5;
while ([data length] > 1000 && resizeAttempts > 0)
{
resizeAttempts -= 1;
compressionRatio = compressionRatio*0.6;
data = UIImageJPEGRepresentation(imagedata,compressionRatio);

}

NSString *baseurl = @"myurl";
NSURL *dataURL = [NSURL URLWithString:baseurl];

NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:500.0];
[dataRqst setHTTPMethod:@"POST"];

NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];

[dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];

NSMutableData *postBody = [NSMutableData data];

// -------------------- ---- image Upload Status -----------------------
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.jpeg",theDate]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:data];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataRqst setHTTPBody:postBody];

self.conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self];

//SubmitDetailsViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"SubmitDetailsViewController"];
// [self.navigationController pushViewController:presales animated:YES];

}
#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self->recieveData = [[NSMutableData alloc] init];
}

//------------- Response from server -------------
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[ self->recieveData appendData:data];

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];

}

最佳答案

您可以使用 AFNetworking库来做到这一点。将 AFNetworking 添加到您的项目并将 AFNetworking.h 导入到您的类中,然后您可以执行以下操作:

  UIImageView *imageView;  // your image view

NSData *imgData = UIImageJPEGRepresentation(imageView.image, 1.0);

NSString *base64ImageStr = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSString *strToSend = [base64ImageStr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSDictionary *parameters = @{@"yourServerKeyOrParameterForImage" : strToSend}; // you can add other parameters as well

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[[manager POST:@"your url" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

NSLog(@"your response : %@",responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"error : %@",error.localizedDescription);

}]resume];

关于ios - 在 IOS 中将图像发布到 PHP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43972291/

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