iOS: Create PDF from UIView

后端 未结 3 1912
心在旅途
心在旅途 2021-02-04 22:08

I am trying to create a PDF from a UIView which may contain images, labels, etc. I have checked the reference ( GeneratingPDF) and I am also aware of the rend

3条回答
  •  礼貌的吻别
    2021-02-04 23:01

    You do indeed have to draw the texts/images individually into a CGContextRef (look at CGPDFContextCreateWithURL) using functions like CGContextShowTextAtPoint. While you could theoretically scan your UIView for images/labels, it would be better if you just draw the layout from scratch. You must have some way to know what elements should be in the UIView. Although the code will be very different, logically you should approach it the same way you would if you were going to draw the UIView programmatically instead of loading it from a nib.

    If you really want to scan your UIView, you could do something like this:

    for(UIView *subview in parentView.subviews) {
     if([subview isKindOfClass:[UIImageView class]]) {
      ...
     } else if([subview isKindOfClass:[UITextView class]]) {
      ...
     } else if([subview isKindOfClass:[UILabel class]]) {
      ...
     }
    }
    

提交回复
热议问题