Каталог не создается - PullRequest
       6

Каталог не создается

0 голосов
/ 03 марта 2012

Я не уверен, почему этот код не работает. Получается, что каталог существует, но потом он утверждает, что его нет.

+(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"}

1 Ответ

1 голос
/ 03 марта 2012

В качестве ответа на ваш предыдущий вопрос я дам вам код, который поможет вам отладить эту проблему.

Попробуйте изменить:

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]);
    }
}

Я предполагаю, что "Изображения" - это не каталог.

...