我想将图像发送到运行.Net的Web服务器,而不是Restful。
我已经尝试了很多东西来将图像转换为字符串然后发送它。
就像使用initWithData:encoding:并尝试通过此问题中给出的方法将图像数据转换为base64encodedstring < / p>
iPhone to MS SQL Image Data Type Conversion Question
但没有运气。
然后我想在facebook api上查看在Facebook墙上上传图片。
使用本教程,图片可以上传到Facebook
但问题是,它使用asihttprequest在Facebook墙上发送数据,我无法看到请求中的原始数据将上传图像。任何人都可以帮助我。
答案 0 :(得分:2)
我在我的最后尝试过。首先我转换为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后,我使用此代码段将图像发送到服务器:
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];
}