将图像发送到Web服务

时间:2012-01-24 12:28:15

标签: iphone xml web-services image base64

我想将我的应用程序中的图像发送到Web服务,为此,我发送数据就像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];

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

2 个答案:

答案 0 :(得分:4)

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

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

现在,将此base64string发送到您的Web服务。

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;
}

答案 1 :(得分:1)

我尝试了Raj发布的代码作为答案,并且非常适合我的iPhone应用程序。

但后来我意识到你可以用一行代码做同样的事情:

NSString* base64string = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];

将base64字符串转换回二进制文件:

NSData* imageData = [[NSData alloc] initWithBase64EncodedString:base64string options:0];

啊呀。

一次,使用Objective-C,这比预期的要容易......!