“操作无法完成。 (可可错误512。)“ [英] "The operation couldn’t be completed. (Cocoa error 512.)"

查看:191
本文介绍了“操作无法完成。 (可可错误512。)“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,它应该可以正常工作,但我不能完全理解为什么它不是:

I have this code, which should be working perfectly, but I can't udnerstand why it isn't:

+(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时,它给我这个错误:

It passed the directoryExists without an error, but when it gets to writeToFile, it gives me this error:

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

为什么会出现这种情况?

Any ideas why this might be?

推荐答案

我首先在路径 @Documents / Images /中编写文件时能够重现错误,然后尝试使用您的代码编写图像。

I was able to reproduce your error when writing a file first in the path @"Documents/Images/", then trying to write the image using your code.

我认为有两种可能的情况:

I think there are two possible scenarios for this:

1)您之前执行的应用程序错误地创建了该文件。如果您使用以下菜单重置模拟器,将解决此问题: iOS模拟器>重置内容和设置,并从您的设备卸载应用程序:长按>单击x符号

1) You created that file by mistake at a previous execution of your app. This will be solved if you reset the simulator using the menu: iOS Simulator > Reset Contents and Settings, and uninstalling the app from your device: Long press > click on the x symbol.

2)您的应用中的其他地方有一些代码可以创建此文件。如果是这种情况,您应该找到此代码并将其删除。

2) There is some code somewhere else in your app that creates this file. If this is the case, you should find this code and remove it.

这篇关于“操作无法完成。 (可可错误512。)“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆