0

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

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

}
4

1 回答 1

1

需要:

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

因为我试图写入路径,但返回的是一个 URL,所以如果你首先在那里创建一个文件,你只能写入路径。

于 2015-06-23T21:12:17.873 回答