ios - How to capture the whole tableview as an image, create a .pdf from it and email it -


this first attempt @ creating .pdf file in ios. have tableview generates data want rendered in .pdf file.

this code capturing whole table image, generating pdf image , emailing it:

- (ibaction)save:(id)sender {     // save table     cgrect frame = self.tableview.frame;     frame.size.height = self.tableview.contentsize.height;     self.tableview.frame = frame;      uigraphicsbeginimagecontextwithoptions(self.tableview.bounds.size, self.tableview.opaque, 0.0);     [self.tableview.layer renderincontext:uigraphicsgetcurrentcontext()];     uiimage *saveimage = uigraphicsgetimagefromcurrentimagecontext();      uigraphicsendimagecontext();     imagedata = uiimagepngrepresentation(saveimage);      uiimage *image = [uiimage imagewithdata:imagedata];     uiimageview *imageview = [[uiimageview alloc]initwithimage:image];       [self createpdffromuiview:imageview savetodocumentswithfilename:filename];    }  - (nsmutabledata*)createpdffromuiview:(uiview*)aview savetodocumentswithfilename:(nsstring*)afilename {     // creates mutable data object updating binary data, byte array     nsmutabledata *pdfdata = [nsmutabledata data];      // points pdf converter mutable data object , uiview converted     uigraphicsbeginpdfcontexttodata(pdfdata, aview.bounds, nil);     uigraphicsbeginpdfpage();     cgcontextref pdfcontext = uigraphicsgetcurrentcontext();       // draws rect view , captured uigraphicsbeginpdfcontexttodata      [aview.layer renderincontext:pdfcontext];      // remove pdf rendering context     uigraphicsendpdfcontext();      // retrieves document directories ios device     nsarray* documentdirectories = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask,yes);      nsstring* documentdirectory = [documentdirectories objectatindex:0];     nsstring* documentdirectoryfilename = [documentdirectory stringbyappendingpathcomponent:afilename];      // instructs mutable data object write context file on disk     [pdfdata writetofile:documentdirectoryfilename atomically:yes];     nslog(@"documentdirectoryfilename: %@",documentdirectoryfilename);     return pdfdata; }  -(ibaction)back:(id)sender {     [self dismissviewcontrolleranimated:yes completion:nil]; }  -(ibaction)email:(id)sender{      mfmailcomposeviewcontroller *mc = [[mfmailcomposeviewcontroller alloc] init];     mc.mailcomposedelegate = self;       uiimage *image = [uiimage imagewithdata:imagedata];     uiimageview *imageview = [[uiimageview alloc]initwithimage:image];       nsmutabledata *pdfdata = [self createpdffromuiview:imageview savetodocumentswithfilename:filename];     // attach image email     [mc addattachmentdata:pdfdata mimetype:@"application/pdf" filename:filename];     // present mail view controller on screen     [self presentviewcontroller:mc animated:yes completion:null];  }  - (void) mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror *)error {     switch (result)     {         case mfmailcomposeresultcancelled:             nslog(@"mail cancelled");             break;         case mfmailcomposeresultsaved:             nslog(@"mail saved");             break;         case mfmailcomposeresultsent:             nslog(@"mail sent");             break;         case mfmailcomposeresultfailed:             nslog(@"mail sent failure: %@", [error localizeddescription]);             break;         default:             break;     }      // close mail interface     [self dismissviewcontrolleranimated:yes completion:null]; } 

i have tested imagedata , capture successful. pdf gets generated single page.

the desired outcome image captured imagedata used create multipage pdf.

how should adapt 'createpdffromuiview' method separate long image file multiple pages using a4 standard paper.

any appreciated

try instead of uigraphicsbeginimagecontextwithoptions

        cgrect priorbounds = self.tableview.bounds;         cgsize fittedsize = [self.tableview sizethatfits:cgsizemake(priorbounds.size.width, self.tableview.contentsize.height)];         self.tableview.bounds = cgrectmake(0, 0, fittedsize.width, fittedsize.height);          cgrect pdfpagebounds = cgrectmake(0, 0, 612, 792); // change need        nsmutabledata *pdfdata = [[nsmutabledata alloc] init];         uigraphicsbeginpdfcontexttodata(pdfdata, pdfpagebounds, nil); {                         (cgfloat pageoriginy = 0; pageoriginy < fittedsize.height; pageoriginy += pdfpagebounds.size.height) {        uigraphicsbeginpdfpagewithinfo(pdfpagebounds, nil);         cgcontextsavegstate(uigraphicsgetcurrentcontext()); {               cgcontexttranslatectm(uigraphicsgetcurrentcontext(), 0, -pageoriginy);                                 [self.tableview.layer renderincontext:uigraphicsgetcurrentcontext()];               } cgcontextrestoregstate(uigraphicsgetcurrentcontext());        }       } uigraphicsendpdfcontext();        self.tableview.bounds = priorbounds; // reset tableview   // use pdfdata           nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);          nsstring *documentspath = [paths objectatindex:0]; //get docs directory                                 filepathpdf = [documentspath stringbyappendingpathcomponent:@"image.pdf"]; //add file name      bool written = [pdfdata writetofile:filepathpdf atomically:yes]; 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -