简体   繁体   English

“这项行动无法完成。 (可可错误512.)“

[英]“The operation couldn’t be completed. (Cocoa error 512.)”

I have this code, which should be working perfectly, but I can't udnerstand why it isn't: 我有这个代码,应该是完美的工作,但我不能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;
}

It passed the directoryExists without an error, but when it gets to writeToFile, it gives me this error: 它传递了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"}

Any ideas why this might be? 任何想法为什么会这样?

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. 在路径@"Documents/Images/"首先写入文件时,我能够重现您的错误,然后尝试使用您的代码编写图像。

I think there are two possible scenarios for this: 我认为有两种可能的情况:

1) You created that file by mistake at a previous execution of your app. 1)您之前执行的应用程序错误地创建了该文件。 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 . 如果您使用以下菜单重置模拟器,将解决此问题: iOS模拟器>重置内容和设置 ,以及从您的设备卸载应用程序: 长按>单击x符号

2) There is some code somewhere else in your app that creates this file. 2)您的应用程序中的某个地方有一些代码可以创建此文件。 If this is the case, you should find this code and remove it. 如果是这种情况,您应该找到此代码并将其删除。

From FoundationErrors.h : 来自FoundationErrors.h

NSFileWriteUnknownError = 512

Try using withIntermediateDirectories:YES . 尝试使用withIntermediateDirectories:YES

In my case a period '.' 在我的情况下,一段时间'。' in the directory name (eg ~/Documents/someDir.dir/somefile) was the cause of the problem. 在目录名称(例如〜/ Documents / someDir.dir / somefile)是问题的原因。 I removed the offending character and the error disappeared. 我删除了违规字符,错误消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该操作无法完成。 (可可错误512.)Coredata Iphone。 没有文件可以复制到文档文件夹中 - The operation couldn’t be completed. (Cocoa error 512.) Coredata Iphone. No files can be copied in documents folder 该操作无法完成。 (可可错误1560.) - The operation couldn’t be completed. (Cocoa error 1560.) 该操作无法完成。 (MKErrorDomain错误4) - The operation couldn’t be completed. (MKErrorDomain error 4) 无法保存到数据存储:无法完成操作。 (可可错误133020.) - Failed to save to data store: The operation couldn’t be completed. (Cocoa error 133020.) 该操作无法完成。 不允许操作 - The operation couldn’t be completed. Operation not permitted DBRequest#connectionDidFinishLoading:将临时文件移动到所需位置时出错:无法完成操作。 (可可错误4.) - DBRequest#connectionDidFinishLoading: error moving temp file to desired location: The operation couldn’t be completed. (Cocoa error 4.) didFailWithError:错误域= kCLErrorDomain代码= 0“无法完成操作。 (kCLErrorDomain错误0。)“ - didFailWithError: Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLErrorDomain error 0.)” 位置管理器错误:操作无法完成。 (kCLErrorDomain错误0。) - Location Manger Error: The operation couldn’t be completed. (kCLErrorDomain error 0.) iOS 5.0 AVAudioPlayer加载音频片段时出错:无法完成该操作。 (OSStatus错误-50。) - iOS 5.0 AVAudioPlayer Error loading audio clip: The operation couldn’t be completed. (OSStatus error -50.) ionic无法加载网页,并显示以下错误:操作无法完成。 (NSURLErrorDomain错误-999。) - ionic failed to load webpage with error: The operation couldn’t be completed. (NSURLErrorDomain error -999.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM