2013-05-07 10 views
1

아래 코드를 사용하여 이미지를 서버에 업로드하고 있습니다.Nsmutabledata가 추가 데이터에서 null이 됨

NSData *imageData = [[[NSData alloc]initWithData:UIImageJPEGRepresentation(m_image.image, 100)]retain]; 

NSString *boundary = @"---------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


//now lets create the body of the post 
//set name here 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
if (imageData) {   
    NSLog(@"length %d",[imageData length]); 
    [body appendData:[NSData dataWithData:imageData]]; 
} 
NSString *bodystring1 = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding]; 
NSLog(@"body : %@",bodystring1); 

출력 : 길이 3826304 몸 : (널) 최대한 빨리 몸은 점점 널 (null)을 이미지를 추가로

. 아무 생각 없어요. 왜 이런 일이 일어 났습니까?

+0

'[NSData appendData :]'를 호출 한 후 어떻게'body'가'nil'이 될 수 있습니까? 그것은'body'가'[appendData :]'가 덮어 쓸 수없는 변수이기 때문에 불가능합니다. – trojanfoe

+0

나는 일어날 수 없다는 것을 알고 있지만 일어난 일입니다. 이미지를 선택하고 업로드하는 것은 매우 간단한 코드입니다. NSMutabledata에 제한이 있습니까? –

+0

당신은 당신의 묘사에 좀더 정확하게 말해야합니다. 디버거 내에서 코드를 실행하고 할당 후 변경되는 부분을 보려면 * watch *'body '를 실행하십시오. 나는 결코 그것을하지 않을 것입니다 ... – trojanfoe

답변

0

문자열 데이터로 이미지 데이터를 조작 할 수 없습니다. 데이터가 손상되어 데이터가 손상 될 수 있으며 적절한 결과를 얻을 수 없습니다. 따라서 뱀과 같은 데이터 묶음으로 문자열과 이미지를 업로드해야합니다.

0

나는 볼 수 없다. 그 시체는 없다. 시체가 아니라 bodystring1을 기록합니다. 아마도 이미지 데이터에 유효하지 않은 UTF8 코드가 들어있을 수 있습니다. -initWithData : encoding : 해당 데이터를 거부하고 nil을 반환합니다.

이미지 데이터를 문자열로 직접 변환하는 것은 좋지 않습니다. Base64 인코딩이나 그런 것을 사용하십시오. 코드 아래

1

시도 :

-(void) uploadImage 
{ 
NSData *imageData = UIImagePNGRepresentation([dic objectForKey:@"image"]); 
// setting up the URL to post to 
NSString *urlString =[NSString stringWithFormat:@"%@YOURURL?User_ID=%@",BaseUrl,  [dicobjectForKey:@"User_ID"]]; 

// setting up the request object now 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 
[request setTimeoutInterval:10]; 

/* 
add some header info now 
we always need a boundary when we post a file 
also we need to set the content type 

You might want to generate a random boundary.. this is just the same 
as my output from wireshark on a valid html post 
*/ 

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

/* 
now lets create the body of the post 
*/ 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"profile_photo\"; filename=\"ipodfile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
// where profile_photo is the key value or parameter value on server. must be confirm 

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

// now lets make the connection to the web 
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; // send data to the web service 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; 
NSString *trimmedString = [returnString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
NSString *trimmedString1 = [trimmedString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; 
trimmedString1 = [trimmedString1 stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 

NSMutableArray *dic0 = [trimmedString1 JSONValue]; 
NSMutableDictionary *dic1 = [dic0 objectAtIndex:0]; 
NSLog(@"Profile photo status : %@",[dic1 valueForKey:@"id"]); 

} 

날 어떤 문제에 대해 알려주십시오.

+0

NSData를 추가 한 후에도 http body가 null이됩니다. –

1

여기 몸에는 경계 값, 내용 정보 및 이미지가 모두 NSData.So로 포함되어 있습니다. 내부적으로 SDK가 본문에서 NSData 형식의 이미지를 인식하지 못하기 때문에 null로만 변환됩니다.이 줄이 반환되는 이유 없는.

관련 문제