gpt4 book ai didi

iphone - 将图像发送到网络服务

转载 作者:数据小太阳 更新时间:2023-10-29 02:58:34 24 4
gpt4 key购买 nike

我想将图像从我的应用程序发送到网络服务,为此,我像 NSData 一样发送数据,但它什么都不做:

NSData *imagen= UIImagePNGRepresentation(windowImage);

Web 服务等待这样的数据:base64Binary

要发送这种类型的数据,我知道我需要使用著名的方法“base64Encoding”将 NSData 转换为 NSString

NSData *imagen=UIImagePNGRepresentation(windowImage);
NSString *encoded= [imagen base64Encoding];

然后我像 SOAP 一样发送数据:

   NSString *soapMsg= [NSString stringWithFormat:............<imagenBase64>%@<imagenBase64>,encoded];

错在哪里?我需要做其他转换吗?

最佳答案

请参阅以下答案。这样做。

NSData *imagen=UIImagePNGRepresentation(windowImage);
NSString *base64String = [self base64StringFromData:imagen length:[imagen length]];

现在,将这个 base64 字符串发送到您的网络服务。

static char base64EncodingTable[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

+ (NSString *) base64StringFromData: (NSData *)data length: (int)length {
unsigned long ixtext, lentext;
long ctremaining;
unsigned char input[3], output[4];
short i, charsonline = 0, ctcopy;
const unsigned char *raw;
NSMutableString *result;

lentext = [data length];
if (lentext < 1)
return @"";
result = [NSMutableString stringWithCapacity: lentext];
raw = [data bytes];
ixtext = 0;

while (true) {
ctremaining = lentext - ixtext;
if (ctremaining <= 0)
break;
for (i = 0; i < 3; i++) {
unsigned long ix = ixtext + i;
if (ix < lentext)
input[i] = raw[ix];
else
input[i] = 0;
}
output[0] = (input[0] & 0xFC) >> 2;
output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
output[3] = input[2] & 0x3F;
ctcopy = 4;
switch (ctremaining) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}

for (i = 0; i < ctcopy; i++)
[result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];

for (i = ctcopy; i < 4; i++)
[result appendString: @"="];

ixtext += 3;
charsonline += 4;

if ((length > 0) && (charsonline >= length))
charsonline = 0;
}
return result;
}

关于iphone - 将图像发送到网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986801/

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