2012-03-03 8 views
-1

このコードが機能しない理由はわかりません。ディレクトリが存在しているが、それ以降は存在しないと主張する。ディレクトリが作成されない

+(NSString *)writeImageToFile:(UIImage *)image { 
    NSLog(@"image 3: %@", image); 
    NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f); 

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

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if (![fileManager fileExistsAtPath:path isDirectory:nil]) { 
     BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]; 
     NSLog(@"success 1: %i", success); 
    } 

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

私のNSLog:あなたの前の質問に対する私の答えとして

image 3: <UIImage: 0x6972820> 
file path: /var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/3323E0F5-0B13-4CBA-898A-252212C65E2F.jpg 
Failed to write to file with error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x6987e20 {NSFilePath=/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/3323E0F5-0B13-4CBA-898A-252212C65E2F.jpg, NSUnderlyingError=0x6977290 "The operation couldn’t be completed. Not a directory"} 
+0

ディレクトリパスにイメージなしで保存しようとしましたか? Imagesディレクトリも作成しますか? – Ravin

+0

ディレクトリ "Images"の作成に使用したコードを投稿できますか?また、私は自分の答えに投稿したコードの結果を投稿できますか? – sch

+0

こんにちは、あなたの問題への解決策を得ましたか?「操作は完了できませんでした。ディレクトリではありません」可能であれば、あなたのソリューションを共有してください。 –

答えて

1

、私はあなたがこの問題をデバッグするのに役立ついくつかのコードを提供します。

は変更してみてください:

if (![fileManager fileExistsAtPath:path isDirectory:nil]) { 
    BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]; 
    NSLog(@"success 1: %i", success); 
} 

へ:

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]; 
    NSLog(@"success 1: %i", success); 
    if (!success) { 
     NSLog(@"Failed to create directory with error: %@", [error description]); 
    } 
} 

私の推測では、 "画像" はディレクトリではないということです。

+0

あなたはそうです、そうではありません。どのように私はディレクトリにそれを作るのですか? – Andrew

+0

"Images"はどこに作成しましたか?そのコードを投稿できますか?ここのコードは正しいように見えるので、別の場所で作成したと思います。 – sch

+0

私はしませんでした。私は、画像をファイルに書き込もうとする前に画像が存在するかどうかをチェックし、そうでない場合は作成します。 – Andrew

関連する問題