writeToFile を使用して pic 画像をドキュメントに保存したいのですが、以下のエラーが見つかりました。解決方法を教えてください。
エラー 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];