writeToFile:返回正确的路径,但没有文件?

writeToFile: returning correct path, but no file?

我有这段代码可以从我为 NSImage 获得的 NSData 制作 PNG,但是没有在下载文件夹中创建文件?

- (IBAction)saveToDownloadsFolder:(id)sender {

    NSInteger selected = [tvContent selectedRow];
    ImageCell *selectedRow = [tvContent viewAtColumn:0 row:selected makeIfNecessary:YES];

    NSURL *documentsURL = [[NSFileManager defaultManager] URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

    CGImageRef CGImage = [selectedRow.item.image CGImageForProposedRect:nil context:nil hints:nil];
    NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease];
    NSDictionary* imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1] forKey:NSImageCompressionFactor];
    NSData *data = [rep representationUsingType: NSPNGFileType properties:imageProps];
    [data writeToFile:[NSString stringWithFormat:@"%@", [documentsURL URLByAppendingPathComponent:@"Copyfeed-Image.png"]] atomically:YES];


    NSLog(@"URL %@", [NSString stringWithFormat:@"%@", [documentsURL URLByAppendingPathComponent:@"Copyfeed-Image.png"]]);

}

需要:

[data writeToURL:[documentsURL URLByAppendingPathComponent:@"Copyfeed-Image.png"] atomically:YES];

因为我试图写入一个路径,但返回了一个 URL,如果您首先在那里创建一个文件,您似乎只能写入路径。