0

我是我的 iphone 应用程序,我想将图像发布到链接。该图像是从手机图库中选择的。我将该图像转换为nsstring。但我在发帖时出错。

错误是:(“错误域=org.brautaset.JSON.ErrorDomain Code=3 \"无法识别的前导字符\" UserInfo=0x9e599a0 {NSLocalizedDescription=无法识别的前导字符}")

我正在使用以下代码

用于将图像转换为字符串:

CGFloat compression = 0.25f;

NSData *imagedata = UIImageJPEGRepresentation(self.profileImageView.image, compression);

profileObj.profileImageString = [[NSString alloc]initWithData:imagedata encoding:NSASCIIStringEncoding];
profileObj.profileImageString = [profileObj.profileImageString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"profileObj.profileImageString: %@",profileObj.profileImageString);

发布到链接:

appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

ProfileObject *profile_obj=obj;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@upload/register/john@gmail.com/password/774e4b7b118994b1c58d71c088834d43ca2937623319f6b7b6c48a1846132027/iPhone/1/%@",MainUrl,profile_obj.profileImageString]];

NSLog(@"url is---%@",url);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
NSError* error = nil;

NSURLResponse* response;

NSData* result = [NSURLConnection sendSynchronousRequest:request  returningResponse:&response error:&error];

NSString *dataString=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
NSMutableDictionary *getResponseDict = [[NSMutableDictionary alloc] init];
[getResponseDict addEntriesFromDictionary:[dataString JSONValue]];
NSString  *responseStr=[getResponseDict objectForKey:@"message"];
NSLog(@"responseStr is....%@",responseStr);
return responseStr;

对不起,我的英语很差 别介意。

提前致谢

4

2 回答 2

1

用于图片发布

NSString  *imgPath = fullPathOfYourImage;
    if([[NSFileManager defaultManager] fileExistsAtPath:imgPath])
    {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];                                                        
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"YourImageName.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                                                                                                               ^..YourImageName….^

        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithContentsOfFile:imgPath]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
于 2013-02-08T08:09:09.613 回答
1

您始终可以对图像数据进行 base64 编码。

这可确保您在帖子中获得正确的字符串。

你可以使用这样的扩展:http ://svn.cocoasourcecode.com/MGTwitterEngine/NSData+Base64.m来获得你想要的。

于 2013-02-08T07:45:46.117 回答