Base 64编码随机行为UIImage

时间:2011-12-13 11:23:55

标签: iphone encoding uiimage

我通过使用....使用Base64方案将NSData对象转换为NSString对象,从而将图像发送到服务器。

 NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
 NSString *encodedString=[Base64Coder encodeData:imgData];

我正在观察一个随机行为....有时我在字符串末尾的编码字符串中得到“==”而图像没有上传。字符串之间也可能还有一些其他字符。如果我不要在最后得到这些字符...图像上传。 为了克服这一点......我还使用这种方法将这些字符转换为有效的(假设被接受)::

-(NSString *)urlEncodedVersion:(NSString *)strString
{ 
    NSMutableString *strTemp = [[NSMutableString alloc] initWithFormat:@"%@",strString] ;
    NSArray *escapeChars = [NSArray arrayWithObjects:@";",@"?",@":",@"@", @"&",@"=",@"+",@"$",@",", @"[",@"]",@"#",@"!",@"’",@"(", @")",@"*",@" ",nil];

    NSArray *replaceChars = [NSArray arrayWithObjects: @"%3B",@"%3F",@"%3A",
                             @"%40",@"%26",@"%3D", @"%2B",@"%24",@"%2C",@"%5B",@"%5D", @"%23",@"%21",@"%27", @"%28",@"%29",@"%2A",@"%20",nil];

    //NSMutableString *tempStr = [[self mutableCopy] autorelease]; 
    for(int i = 0; i < [escapeChars count]; i++)
    {
        [strTemp replaceOccurrencesOfString:[escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0,[strTemp length])];
    } 
    return strTemp;
}

但它也没有服务。

这是我整个帖子的主体::

-(void)uploadProfileInfo:(CreateProfile *)objCP
{

    NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
    NSString *encodedString=[Base64Coder encodeData:imgData];

    NSString *refinedString=[self urlEncodedVersion:encodedString];

    NSString *soapMessage = [NSString stringWithFormat:

                             @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             "<soapenv:Envelope \n"
                             "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
                             "xmlns:tem=\"http://tempuri.org/\" \n"
                             "xmlns:wcf=\"http://schemas.datacontract.org/2004/07/NextToMe_BusinessEntity\"> \n"
                             "<soapenv:Header/>\n"
                             "<soapenv:Body>\n"
                             "<tem:CreateProfile>\n"

                             "<tem:objUser>\n"

                             "<wcf:Email>%@</wcf:Email>\n"

                             "<wcf:Mode>%@</wcf:Mode> \n"

                             "<wcf:Name>%@</wcf:Name>\n"

                             "<wcf:ProfileImage>%@</wcf:ProfileImage>\n"

                             "<wcf:RequestDateTime>%@</wcf:RequestDateTime>\n"

                             "<wcf:Status>%@</wcf:Status>\n"

                             "<wcf:StatusSpecified>%@</wcf:StatusSpecified>\n"

                             "<wcf:UDID>%@</wcf:UDID>\n"

                             "</tem:objUser>\n"

                             "</tem:CreateProfile>\n"

                             "</soapenv:Body>\n"

                             "</soapenv:Envelope>\n",[objCP email],[objCP mode],[objCP name],encodedString,@"",[objCP status],[objCP statusSpecified],[objCP UDID]];





    NSURL *url = [NSURL URLWithString:kBaseURL];               
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];             
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];          
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
    [theRequest addValue: @"http://tempuri.org/XXXX/CreateProfile" forHTTPHeaderField:@"Soapaction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"The Connection is NULL");
    }

}

1 个答案:

答案 0 :(得分:1)

你在最后看到的=字符是填充。这是根据RFC4648 3.2。填充编码数据。另一端应该能够消化它。显然这没有发生。您应该将行为与第三方库进行比较,以检查编码或解码是否错误。如果你可以删除填充,那么对你有好处,但这是解码库中的一个错误。