我的iPhone應用程序我想發佈圖像鏈接。該圖像是從手機圖庫中選擇的。我將該圖像轉換爲nsstring。但是我發佈時發生錯誤。我如何使用json解析將選定圖像發佈到網頁鏈接
錯誤是:( 「錯誤域= org.brautaset.JSON.ErrorDomain代碼= 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/[email protected]/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;
對不起,我英語不好不介意。
在此先感謝
看看我答,同樣的問題 http://stackoverflow.com/questions/10524323/converting-image-to-base64-and-uploading-in- json-format-to-server/14605101#14605101 它會爲你工作。 – aBilal17