【问题标题】:How to print raw data of ASIHTTPRequest如何打印 ASIHTTPRequest 的原始数据
【发布时间】:2011-09-12 08:46:18
【问题描述】:

我想将图像发送到运行 .Net 而不是 Restful 的网络服务器。

我已经尝试了很多方法将图像转换为字符串然后发送它。
就像使用 initWithData:encoding: 一样,还尝试通过此问题中给出的方法将图像数据转换为 base64encodedstring p>

iPhone to MS SQL Image Data Type Conversion Question

但没有运气。

然后我想在 facebook api 上查看用于在 facebook 墙上上传图像。
使用本教程可以将图像上传到 facebook

http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from-your-iphone-app

但问题是,它使用 asihttprequest 在 facebook 墙上发送数据,我看不到请求中的原始数据将上传图像。谁能帮帮我。

【问题讨论】:

  • 原始数据是什么意思? ASIHTTPRequest 的委托方法让您了解正在发生的事情。
  • 委托方法只告诉我们它正在向数组类型的对象添加一个文件路径,然后进行异步调用以将数据发送到网络服务器,但它没有告诉我们它究竟是如何发送的数据,例如在服务器上发送的数据格式或类型。
  • 在 ASIHTTPRequestConfig.h 中,将 DEBUG_FORM_DATA_REQUEST 设置为 1,它将调试数据记录到控制台。

标签: iphone objective-c ios asihttprequest image-uploading


【解决方案1】:

我最后试过了。首先我像这样转换为base64encoding:

NSString *str64;

        if(Image){
            NSData *imageData = UIImageJPEGRepresentation(Image,0.75);
            str64 = [imageData base64Encoding];
        } 

然后我添加到一个字典中,然后将该字典添加到一个数组中,以便我可以将它作为 JSON 字符串传递到请求的正文中。

NSMutableArray *ParaArray =[[NSMutableArray alloc]init];

    NSDictionary *ParaDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSString stringWithFormat:@"%@",Userid], @"userid",
                                  [NSString stringWithFormat:@"%@",[Imagename URLEncodedString]], @"imagename",
                                  [NSString stringWithFormat:@"%@",DateNTime], @"datetime", 
                                  [NSString stringWithFormat:@"%@",ImageLocation], @"imagelocation",
                                  str64,@"image",
                                  nil]; // set the parameter


    [ParaArray addObject:ParaDictionary];

在使用 ASIFormDataRequest 之后,我使用这个 sn-p 将图像发送到服务器:

ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]];

    [request setPostValue:[ParaArray JSONRepresentation]  forKey:@"bulk_data"];

    //[ParaDictionary release];
    [ParaArray release];

    [request setTimeOutSeconds:20];


    [request startSynchronous];

对我来说效果很好。我希望它也能帮助你。

数据以字符串的形式传递,我们可以通过复制粘贴直接使用base64Encoding方法。尽管您可能知道他们还在这里,但他们仍然存在:

- (NSString *) base64Encoding {
return [self base64EncodingWithLineLength:0];
}

- (NSString *) base64EncodingWithLineLength:(NSUInteger) lineLength {
const unsigned char *bytes = [self bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
unsigned long ixtext = 0;
unsigned long lentext = [self length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
unsigned short i = 0;
unsigned short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;

while( YES ) {
    ctremaining = lentext - ixtext;
    if( ctremaining <= 0 ) break;

    for( i = 0; i < 3; i++ ) {
        ix = ixtext + i;
        if( ix < lentext ) inbuf[i] = bytes[ix];
        else inbuf [i] = 0;
    }

    outbuf [0] = (inbuf [0] & 0xFC) >> 2;
    outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
    outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
    outbuf [3] = inbuf [2] & 0x3F;
    ctcopy = 4;

    switch( ctremaining ) {
        case 1:
            ctcopy = 2;
            break;
        case 2:
            ctcopy = 3;
            break;
    }

    for( i = 0; i < ctcopy; i++ )
        [result appendFormat:@"%c", encodingTable[outbuf[i]]];

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

    ixtext += 3;
    charsonline += 4;

    if( lineLength > 0 ) {
        if( charsonline >= lineLength ) {
            charsonline = 0;
            [result appendString:@"\n"];
        }
    }
}

return [NSString stringWithString:result];
}

【讨论】:

  • 我不是要求将图像传输到网络服务器的代码,我的问题是通过互联网将确切的数据发送到服务器,我们也可以粘贴称为 base64Encoding 的方法。
  • 数据以字符串的形式通过互联网发送,我们可以复制粘贴以下base64Encoding方法-
  • 是的,当然,这就是全部。对于进一步的操作,您必须在 Web 端处理它。我很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多