gpt4 book ai didi

objective-c - 将图像数组(包装在 NSData 中)上传到 iOS 中的服务器

转载 作者:行者123 更新时间:2023-11-28 20:31:48 25 4
gpt4 key购买 nike

美好的一天,我正在尝试将一组图像上传到服务器,图像被包装到 NSData 中,但服务器只返回一个图像,这是我的代码:

- (void)actionSave{

NSArray *imagesArray = [NSArray arrayWithObjects:[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageOne.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageTwo.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageThree.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageFour.image)], nil];

NSData *images = [NSKeyedArchiver archivedDataWithRootObject:imagesArray];

NSMutableURLRequest *requestImg = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&mid=%@", SEND_POST_TO_WALL, [self.currentFriend objectForKey:@"uid"]]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];

[requestImg setHTTPMethod:@"POST"];

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

NSMutableData *postData = [NSMutableData dataWithCapacity:[images length] + 512];
[postData setData:images];

NSMutableData *body = [NSMutableData data];

//Photo
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:postData];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//Text
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"text\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[self.textView.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[requestImg setHTTPBody:body];

NSURLConnection *connectionForImage = [NSURLConnection connectionWithRequest:requestImg delegate:self];

if (connectionForImage) {
self.infoData = [NSMutableData data];
} else {
NSLog(@"Connection failed");
}

这是服务器返回的结果:

    array(3) {
["act"]=>
string(12) "comments.add"
["module"]=>
string(7) "account"
["mid"]=>
string(4) "7728"
}
POST:

array(1) {
["text"]=>
string(5) "Gjdgy"
}
FILES:

array(1) {
["image"]=>
array(5) {
["name"]=>
string(9) "image.png"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/php9oudKt"
["error"]=>
int(0)
["size"]=>
int(463652)
}
}

我做错了什么???

提前致谢!

最佳答案

您向我们展示了数据的 POST 但没有再次展示该数据的 GET,因此不确定“仅返回一张图像”是什么意思。它返回的应该是你上去的完全相同的数据 block ,也就是存档。您可以通过记录发送的大小和返回的大小来测试这一点 - 如果服务器返回原始 blob,它们应该相同(如果不是,那是网络问题而不是 ios 问题).

对于从网络上取回的 blob,您需要对其进行 NSKeyUnarchive 以获取原始数据项数组。拆开是拆开的反面。

关于objective-c - 将图像数组(包装在 NSData 中)上传到 iOS 中的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900850/

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