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

时间:2015-06-23 21:02:13

标签: nsdata nsurl writetofile cgimageref nsbitmapimagerep

我有这个代码用NSG制作一个PNG,我用于NSImage,但下载文件夹中没有创建文件?

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

}

1 个答案:

答案 0 :(得分:1)

需要:

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

因为我试图写入路径,但是返回了一个URL,所以只能在第一次创建文件时写入路径。

相关问题