-2

UIViewをPDFに変換したいのですが、それを行う時間が少ないので、そこに既製のコンポーネントはありますか。そうでない場合は、PDFのサンプルコードを教えてください

前もって感謝します

4

4 に答える 4

6
(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

また、必ずインポートしてください:QuartzCore / QuartzCore.h

于 2012-05-07T06:57:28.953 に答える
0

QuartzDemoのAppleSampleCode `をクリックすると、uiviewsでPDFやさまざまな図について学ぶことができます。

于 2012-05-07T07:38:30.063 に答える
0

または、このhttps://github.com/vfr/Reader PDF Reader/Viewerを試してください

于 2012-05-07T08:14:24.450 に答える
0

GitHubへのPDFGenerator リンクを使用できます:

また、githubにデモアプリケーションがあります。

コードフォントのサンプル:ビューからのPDFの生成

func generatePDF() {
    let v1 = UIScrollView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    let v3 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v1.backgroundColor = .red
    v1.contentSize = CGSize(width: 100.0, height: 200.0)
    v2.backgroundColor = .green
    v3.backgroundColor = .blue

    let dst = URL(fileURLWithPath: NSTemporaryDirectory().appending("sample1.pdf"))
    // outputs as Data
    do {
        let data = try PDFGenerator.generated(by: [v1, v2, v3])
        data.write(to: dst, options: .atomic)
    } catch (let error) {
        print(error)
    }

    // writes to Disk directly.
    do {
        try PDFGenerator.generate([v1, v2, v3], to: dst)    
    } catch (let error) {
        print(error)
    }
}
于 2017-05-03T18:47:58.423 に答える