错误域= NSCocoaErrorDomain代码= 512“操作无法完成。操作无法完成。是一个目录 [英] Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed.The operation couldn’t be completed. Is a directory

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

问题描述

我想使用writeToFile将pic图像存储到文档中,但是找到下面的错误,如何解决?

I want use writeToFile to store a pic image to the documents, but find the error below, how to solve it ?

错误域= NSCocoaErrorDomain代码= 512操作无法完成。(Cocoa错误512.){NSFilePath = / Users / alexqdh / Library / Application Support / iPhone Simulator / 6.0 / Applications / xxx / Documents / girl.png,NSUnderlyingError = 0x753e630操作无法完成。目录是}

-(NSString*)getImagePath:(NSString *)name
{
    NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *docPath = [path objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *finalPath = [docPath stringByAppendingPathComponent:name];
    [fileManager createDirectoryAtPath:finalPath withIntermediateDirectories:YES attributes:nil error:nil];
    return finalPath;
}
//below is how to use NSData writeToFile
AFImageRequestOperation *opera = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    img.image = image;
    [self.view addSubview:img];
    NSData *imageData = UIImagePNGRepresentation(img.image);
    if(imageData == nil)
    {
        NSLog(@"imageData nil");
    }
    NSString *picPath = [self getImagePath:@"girl.png"];
    NSLog(@"path:%@",picPath);
    NSError *error = [[NSError alloc]init];
    BOOL a = [imageData writeToFile:picPath options:NSDataWritingAtomic error:&error];
    if(a == YES)
    {
        NSLog(@"YES");
    }
    else
    {
        NSLog(@"NO.error:%@", error);
    }

    /*NSData *imageData = UIImagePNGRepresentation(img.image);
    NSString *picPath = [self getImagePath:@"girl.png"];
    [imageData writeToFile:picPath atomically:YES];*/

    NSLog(@"Got it.code:%d",response.statusCode);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Failed pic.error:%@",error);
}];

[opera start];


推荐答案

问题是由创建名称的目录引起的的图像,然后尝试写一个文件来代替目录。换句话说,您首先要创建一个名为的目录:

The problem is caused by creating a directory with the name of the image, then trying to write a file in place of the directory. In other words, you are first creating a directory named:


Documents / girl.png /

Documents/girl.png/

然后你尝试写一个文件到路径:

then you try to write a file to the path:


文件/女孩。 png

Documents/girl.png

显然这不是你想要做的。在 getImagePath:方法中,删除调用以创建目录。 Documents 目录已存在。当然,您不需要与文件名对应的目录。

Obviously this is not what you meant to do. In your getImagePath: method, get rid of the call to create the directory. The Documents directory will already exist. And of course you don't want a directory corresponding to the filename.

另一个选项是更改 getImagePath: to:

One other option is to change your getImagePath: to:

- (NSString*)getImagePath:(NSString *)name {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *docPath = [path objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *finalPath = [docPath stringByAppendingPathComponent:name];

    // Remove the filename and create the remaining path
    [fileManager createDirectoryAtPath:[finalPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];

    return finalPath;
}

此更新后的代码现在支持您传入 name 包含您可能需要的子目录的值。

This updated code now supports you passing in name values that include subdirectories you might want.

这篇关于错误域= NSCocoaErrorDomain代码= 512“操作无法完成。操作无法完成。是一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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