45147

误差区域= NSCocoaErrorDomain代码= 512 \“的操作不能完成进度操作无法完成。

我想用将writeToFile到PIC图像存储的文件,却发现下面的错误,怎么解决呢?

错误域= NSCocoaErrorDomain代码= 512 “的操作无法完成。(可可错误512)”{NSFilePath = /用户/ alexqdh /库/应用程序支持/ iPhone模拟器/ 6.0 /应用/ XXX /文件/ 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];

Answer 1:

这个Question是通过创建与图像的名称的目录,然后试图写一个文件来取代目录引起的。 换句话说,你首先创建一个名为:

文件/ girl.png /

然后尝试写入文件的路径:

文件/ 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值,其中包括你可能想子目录。



文章来源: Error Domain=NSCocoaErrorDomain Code=512 \"The operation couldn’t be completed.The operation couldn’t be completed. Is a directory

Recommend

  • InDesign CS5 Script: Why is `#targetengine` not working correctly?
  • Similar VBScript for converting Excel and PowerPoint to PDF
  • nest for elasticsearch version 2.0.0.0 partial update on nested objects
  • Couldn`t join two files with one key via Cascading
  • when I use git clone to get a git from the remote, the git_index_entrycount return 0?
  • C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries
  • Nested Bulleted lists in Novacode docx
  • Custom Check mark for selected Row
  • dispatch_sync inside a dispatch_async
  • Delete all files and folder from a certain folder
  • Can't access TabBarController from ImageView
  • How to compare two NSMutableArrays?
  • iPhone SDK: custom buttons in TableView
  • How to set background color in CCLabelTTF in cocos2d
  • dispatch_sync inside a dispatch_async
  • Random Text Sent to UILabel
  • UITableView delegates not getting called after NSURL delegates
  • Perform Segue after clicking on search result
  • Potential Memory Leak for ABRecordCopyValue
  • I am unable to save and retrieve image in iOS
  • looping through subviews or a view
  • Out of Bounds Exception on NSArray thats Empty
  • Disable autorotate for view in tabview controller
  • UITableView scroll to specific section using UIPIckerView?
  • tracksWithMediaType returns 0 tracks sometimes
  • Compare two arrays and put equal objects into a new array [duplicate]
  • UITableView not displaying parsed data
  • How to fetch data from pList in Label
  • Creating a file name by adding two NSStrings
  • Load audio in DOCUMENTS when UITableView “cell” is pressed
  • Adding custom gesture to iCarousel items
  • Date formatter for converting 14-sept-2011 in 14th Sept
  • TouchesEnded not working in iOS5, working fine in iOS4
  • Store and fetch documents in/from app documents folder
  • NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for em
  • Display Local HTML file from documents directory in a UIWebView on iPhone
  • -[__NSArrayM insertObject:atIndex:]: object cannot be nil
  • UITableView In Edit Mode - Pressing Delete Makes My App Crash
  • Why won't my UITableViewCell deselect and update its text?