在我的一项活动中,我在屏幕上使用WebView显示了PDF,并尝试使用以下代码保存此PDF:
_pageSize = CGSizeMake(width, height);
NSString *newPDFName = [NSString stringWithFormat:@"%@", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
NSLog(@"path=%@",pdfPath);
当我在iOS模拟器中运行此代码(使用Xcode)并显示路径时,我在文档文件夹中成功打开了此PDF文件,但是当我在iPhone中运行此代码时,我得到了这条路径:
/var/mobile/Applications/0AF98361-C8DF-4C35-9E9F-EE48555185BC/Library/354746396.pdf
那么存储在iPhone中的PDF文件在哪里?
答案 0 :(得分:3)
" .. InDomains( NSLibraryDirectory ,NSUserDomainMask,YES);"
" ../的库强> /354746396.pdf"
您的路径正在查看NSLibraryDirectory
,它应该像这样查看NSDocumentDirectory
:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
如果仍然失败,请尝试以下代码:
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:@"filename.pdf"];
NSLog(@"savePath: %@", savePath);
答案 1 :(得分:3)
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
答案 2 :(得分:0)