gpt4 book ai didi

iphone - 为什么我的文件没有保存?

转载 作者:行者123 更新时间:2023-12-01 18:32:20 25 4
gpt4 key购买 nike

我正在使用一些代码从 UIWebView 保存 png 文件。它不起作用,按下“保存”按钮时没有任何 react 。除了“正在保存文件...”之外,日志中什么也没有出现,这只是为了测试它是否在 Interface Builder 中正确链接(确实如此)。

这是我正在使用的代码:

-(IBAction)saveFile 
{

NSLog(@"Saving File...");

NSURL *requestedURL = [webView.request URL];

if([[requestedURL pathExtension] isEqualToString:@"png"])
{
NSData *fileData = [[NSData alloc] initWithContentsOfURL:requestedURL];
//Store the data locally
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[requestedURL lastPathComponent]];

NSError *error = nil;
[fileData writeToFile:filePath options:NSDataWritingFileProtectionComplete error:&error];

if(error != nil) {
// Failed to write the file
NSLog(@"Failed to write file: %@", [error description]);
} else {
UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString
stringWithFormat:@"The file %@ has been saved", [requestedURL lastPathComponent]] delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[filenameAlert show];
[filenameAlert release];
}
}

最佳答案

似乎您尝试写入 mainBundle 的文件位置(设备上不允许/可能)。尝试使用查找文档目录

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES);
NSString* resourceDocPath = [paths objectAtIndex:0];

关于iphone - 为什么我的文件没有保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com