我的项目中有PDF。我想提供将该文件下载到移动目录并将其与其他应用程序一起加载的功能。
答案 0 :(得分:1)
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/RTOFiles"];;
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDir])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDir withIntermediateDirectories:NO attributes:nil error:&error];
NSString *pdfName = @"form_1.pdf";
NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName];
//Using NSFileManager we can perform many file system operations.
BOOL success = [fileManager fileExistsAtPath: docPdfFilePath];
if (!success) {
NSString *samplePdfFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName];
success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error];
if (!success)
// NSAssert1(0, @"Failed to copy file ‘%@’.", [error localizedDescription]);
NSLog(@"Failed to copy %@ file, error %@", pdfName, [error localizedDescription]);
else {
NSLog(@"%@ File copied to %@", pdfName,documentsDir);
}
}
else{
NSLog(@"File already Exists !");
}
答案 1 :(得分:1)
如果您的图片位于app bunldle中,则可以直接使用您的图片,
UIImage *img = [UIImage imageNamed:@"Amy.png"];
如果您想从应用套装中pdf
加载webview
,那么您可以执行类似的操作,
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPdfName" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
简而言之,如果您在应用程序包中有可用的文件,则无需将其复制到documentDirectory
,因为它将使用双内存,您可以直接从捆绑中使用它。