Error Domain=NSCocoaErrorDomain Code=512 \"The ope

2019-04-17 05:55发布

问题:

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

Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" {NSFilePath=/Users/alexqdh/Library/Application Support/iPhone Simulator/6.0/Applications/xxx/Documents/girl.png, NSUnderlyingError=0x753e630 "The operation couldn’t be completed. Is a directory"}

-(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];

回答1:

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/

then you try to write a file to the path:

Documents/girl.png

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.

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

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



标签: ios ios5 nsdata