gpt4 book ai didi

ios - 错误域=NSCocoaErrorDomain 代码=512 "The operation couldn’ t be completed.The operation couldn't be completed.是一个目录

转载 作者:行者123 更新时间:2023-12-01 17:33:03 25 4
gpt4 key购买 nike

我想使用writeToFile 来存储图片到文件中,但是发现下面的错误,如何解决?

Error Domain=NSCocoaErrorDomain Code=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];

最佳答案

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

Documents/girl.png/

然后您尝试将文件写入路径:

Documents/girl.png

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

另一种选择是将 getImagePath: 更改为:

- (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 值。

关于ios - 错误域=NSCocoaErrorDomain 代码=512 "The operation couldn’ t be completed.The operation couldn't be completed.是一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com