簡體   English   中英

如何在iPhone中檢索PDF

[英]How to retrieve PDF in iPhone

我正在嘗試使用NSSearchPathForDirectoriesInDomains檢索PDF文件。我正在獲取PDF文件的路徑,但是如何將該PDF文件轉換為nsdata或nsmutabledata(bytes)。這是我獲取PDF路徑的代碼。

NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path1 = [arrayPaths1 objectAtIndex:0];
path1 = [path1 stringByAppendingPathComponent:@"mypdffile.pdf"];

在path1中,我正在獲取路徑並獲取我正在嘗試的字節數據

[decodedData writeToFile:path1 atomically:YES];

encodedData為NSMutableData.decodedData為nil。請幫幫我,如何解決這個問題?

為此,您可以使用[NSData dataWithContentsOfFile:<#path of your file#>]

在您的情況下,應為:

decodedData = [NSData dataWithContentsOfFile: path1];

希望能幫助到你。

嘗試下面的代碼。

 NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url];

    //Get path directory
    NSArray *arrayPaths1 =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    //Create PDF_Documents directory
    NSString * documentsDirectory = [arrayPaths1 objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"mypdffile.pdf"];

    [[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"PDFRefYouPreferred"];

    [dataPdf writeToFile:filePath atomically:YES];

希望它能解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM