2015-01-19 17 views
1

안녕하세요, 내 응용 프로그램에서 pdf 파일을 다운로드하고 총 크기가 청크가됩니다. 이제 그 데이터를 청크로 가져온 후에 NSData 객체에 저장하고 남은 청크를 같은 객체에 추가하려고합니다. 이 응용 프로그램을하는 동안 낮은 메모리 경고와 충돌지고있다. 다음에 디스크에 데이터를 쓰면 샌드 박스에 기록 된 파일에 데이터를 추가 할 수 있습니다. 언젠가 파일은 400Mb 이상입니다. 제발 도와주세요.ios에서 디스크에 청크로 데이터를 쓰는 방법

답변

2

NSFileHandle이 사용할 수 있습니다 사용할 수 있습니다

이 같은

뭔가 :

1 단계 : 바르가 _outputFileHandle라고 만들기;

NSFileHandle *_outputFileHandle; 

2 단계 다음 prepareDataHandle 한 번 호출 :

3 단계 :. 데이터 정크가 오는 때마다 writingDataToFile를 호출

파일 다운로드가 완료 될 때 알 수 있도록 작업을 따라 흘러 수정합니다.

-(void)prepareDataHandle 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"anoutputfile.xxx"]; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath] == NO) 
    { 
     NSLog(@"Create the new file at outputFilePath: %@", outputFilePath); 
     BOOL suc = [[NSFileManager defaultManager] createFileAtPath:outputFilePath 
               contents:nil 
              attributes:nil]; 
     NSLog(@"Create file successful?: %u", suc); 
    } 
    _outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:outputFilePath]; 
} 

-(void)writingDataToFile:(NSData *)dataToWrite 
{ 
    if (dataToWrite.length != 0) 
    { 
     [_outputFileHandle writeData:dataToWrite]; 
    } 
    else //you can use dataToWrite with length of 0 to indicate the end of downloading or come up with some unique sequence yourself 
    { 
     NSLog(@"Finished writing... close file"); 
     [_outputFileHandle closeFile]; 
    } 
}