2012-03-04 2 views
2

파일 Blob을 Webservice에서 NSData Object로 변환하는 데 문제가 있습니다. File Blob은 모든 유형의 파일이 될 수 있습니다. 나는 RestKit를 통해 웹 서버에서 파일을 요청하고 다음과 같은 응답을 얻을 수 있습니다 :파일 Blob을 NSData로 변환하는 방법?

[{ 
    "FileBlob": [ 
     65, 
     108, 
     108, 
     111, 
     99, 
     46, 
     32, 
     83, 
     /* more integer values omitted */ 
    ], 
    "FileID": 1234567890, 
    "FileName": "name.txt" 
}] 

내가 잘 작동 JSONKit NSDictionary *dict = [[response bodyAsString] objectFromJSONString];를 통해 사전에 응답을 변환합니다. 하지만 FileBlob을 NSData Object로 변환하여 HDD에 파일로 쓸 수있는 방법을 알아낼 수 없습니다. 파일이있는 경우 내가 배열 물마루 가서 변환 할 수 있습니다 위의 예에서와 같이 TXT 파일의 경우 모든 정수 값은

NSMutableString *fileString = [NSMutableString string]; 
for (NSNumber *value in [[dict valueForKey:@"FileBlob"] objectAtIndex:0]) { 
    [fileString appendFormat:@"%c", (char)value.integerValue]; 
} 

을가 문자 다음 디스크

NSData *data = [fileString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent:filename];   
[[NSFileManager defaultManager] createFileAtPath:file contents:data attributes:nil]; 

에 모든 것을 저장하지만, pdf와 같은 다른 기능은 작동하지 않습니다. File Blob을 NSData Object로 변환하거나 디스크에 파일로 blob을 쓰는 방법이 있습니까? 대신

답변

0

사용 NSMutableData :

NSMutableData *data = [NSMutableData dataWithCapacity:blobs.count]; 

for (NSNumber *number in blobs) 
{ 
    uint8_t byte = (uint8_t)[number intValue]; 
    [data appendBytes:&byte length:1]; 
} 

// write data to file 
+0

와우는 당신의 도움을 주셔서 너무 감사합니다. 귀하의 제안은 완벽하게 작동합니다. – lahmar

관련 문제