2013-08-18 1 views
3

NsData 파일을 디렉토리에 저장하려고합니다. 내가 작성하려고 URL의 예 (fileToWrite)입니다NSdata writeToURL이 작동하지 않습니다.

- (void)cacheImage:(NSData *)imageData withTitle:(NSString *)title 
{ 

    NSURL *cacheURL = (NSURL *)[[self.fileMenager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] objectAtIndex:0]; //we take the URL of the cache Directory and comvert the url of the cache directory into a string 
    NSURL *imageFolder = [cacheURL URLByAppendingPathComponent:PHOTO_CACHE_FOLDER]; 

    if ([self.fileMenager isWritableFileAtPath:[cacheURL path]]) { //check if the cache directory is writable 
     if (![self.fileMenager createDirectoryAtURL:cacheURL withIntermediateDirectories:NO attributes:nil error:nil]) { //check if the directory of our image is already exist 
      NSURL * fileToWrite = [[imageFolder URLByAppendingPathComponent:title isDirectory:NO] URLByAppendingPathExtension:@"jpg"]; //take the complete url of image 
      if ([imageData writeToURL:fileToWrite atomically:YES]) //atomically = safe write 
      { 
       NSArray *debug = [self.fileMenager contentsOfDirectoryAtPath:[imageFolder path] error:nil]; 
       for (NSURL *url in debug) 
        NSLog(@"url prooooo : %@",url); 
      } 
     } 
    } 
} 

:

file://localhost/Users/MarcoMignano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/93B0A0AC-EC13-4050-88CA-F46FBA11001E/Library/Caches/image_cache/Mozart%201.jpg 

요점은 간단하다이 방법은 내가 할 수있는, NO 돌아 writeToURL 그건 내 방식의 코드 왜 그 URL이 올바른지 이해하지 못합니다.

누군가 내 도움을 줄 수 있습니다. ' 감사합니다.

+0

디버그하고 얼마나 멀리 도달했는지 확인 했습니까? 'imageData'는 non-nil입니까? – Wain

답변

2

주된 문제점은 잘못된 디렉토리를 생성하고 있다는 것입니다. imageFolder 디렉토리를 만들어야합니다.

if ([self.fileManager createDirectoryAtURL:imageFolder withIntermediateDirectories:YES attributes:nil error:nil]) { 

그리고 당신이 if 문에 !을 원하지 않는 점에 유의.

+0

'error' 매개 변수를 사용하고'createDirectoryAtURL'가 NO를 리턴하면 결과 오류를 기록하십시오. – rmaddy

+0

완벽 지금 모든 작동합니다,하지만 난 응용 프로그램을 삭제하고 다시, 그렇지 않으면 createDirectoryAtURL 반환합니다 다시 감사합니다. –