2012-03-03 2 views
6

". 동작 (코코아 오류 512)을 완료 할 수 없습니다",하지만 난 udnerstand 수 없습니다는 내가 완벽하게 작동해야이 코드를 가지고

+(NSString *)writeImageToFile:(UIImage *)image { 

    NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f); 


    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    BOOL isDirectory = NO; 
    BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory]; 
    if (directoryExists) { 
     NSLog(@"isDirectory: %d", isDirectory); 
    } else { 
     NSError *error = nil; 
     BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error]; 
     if (!success) { 
      NSLog(@"Failed to create directory with error: %@", [error description]); 
     } 
    } 

    NSString *name = [NSString stringWithFormat:@"%@.jpg", [JEntry generateUuidString]]; 
    NSString *filePath = [path stringByAppendingPathComponent:name]; 
    NSError *error = nil; 
    BOOL success = [fullImageData writeToFile:filePath options:NSDataWritingAtomic error:&error]; 
    if (!success) { 
     NSLog(@"Failed to write to file with error: %@", [error description]); 
    } 

    return filePath; 
} 

이 오류없이 directoryExists을 통과하지만 writeToFile에 도달 할 때, 그것은 나에게이 오류 제공 :

Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x5634ee0 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/18DAE0BD-6CB4-4244-8ED1-9031393F6DAC.jpg, NSUnderlyingError=0x5625010 "The operation couldn’t be completed. Not a directory"} 

모든 아이디어는 이유가 될 수 있습니까?

답변

8

처음으로 파일을 쓸 때 @"Documents/Images/" 경로에 오류를 재현 한 다음 코드를 사용하여 이미지를 쓰려고했습니다.

1) 앱의 이전 실행에서 실수로 해당 파일을 생성 :

나는이 두 가지 가능한 시나리오는이에 대해 생각합니다. iOS 시뮬레이터> 콘텐츠 및 설정 재설정 및 기기에서 앱을 제거하는 경우 x 기호을 길게 누릅니다. 메뉴를 사용하여 시뮬레이터를 재설정하면 문제가 해결됩니다.

2) 앱에 어딘가에이 파일을 만드는 코드가 있습니다. 이 경우이 코드를 찾아서 제거해야합니다.

+0

첫 번째 해결책은 맞았습니다. 고마워. :) – Andrew

+0

@sch : 그날 늦게 나도 알아,하지만 고마워. 내 문제는 내 코드가 iPad에서는 잘 실행되었지만 시뮬레이터에서는 제대로 실행되지 못했습니다. 내용 및 설정 재설정으로 문제가 해결되었습니다. – Robert

+1

나는 2) 문제가 있었다. 하지만 처음에는 컴파일러에 영구 저장소가 없다고 말합니다. 이제 영구 저장소를 추가하려고합니다. 이미 가지고 있기 때문에 코코아 오류 512와 함께 실패합니다! 뭐야! – coolcool1994

3

에서 FoundationErrors.h는 :

NSFileWriteUnknownError = 512 

withIntermediateDirectories:YES를 사용해보십시오.

1

내 경우 마침표 '.' (예 : ~/Documents/someDir.dir/somefile)이 문제의 원인이었습니다. 문제의 성격을 제거하고 오류가 사라졌습니다.