PDF Generation From UIScrollView + iphone

后端 未结 3 1656
半阙折子戏
半阙折子戏 2021-01-03 11:08

I want to generate a pdf of UIScrollView whose content size is near about 320*2000. If I use current graphic image context to capture the scroll view layer, then it only cap

3条回答
  •  花落未央
    2021-01-03 11:57

    Heh guys, i prepared some code base using the examples available on net to create PDF from UIScrollView We should be clear about two things

    1) number of pages 2) contentsize of scrollview

    i have created a scroll view of content size ( 10 * 910). so it has 10 pages.. This is for iPad.

    here is the code base

    - (void) createPDF
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *directroyPath = nil;
        directroyPath = [documentsDirectory stringByAppendingPathComponent:@"PDF"];
        NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
        // check for the "PDF" directory
        NSError *error;
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    
        } else {
            [[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
                                      withIntermediateDirectories:NO
                                                       attributes:nil
                                                            error:&error];
        }   
    
         CGContextRef pdfContext = [self createPDFContext:myScrollView.bounds path:(CFStringRef)filePath];
        NSLog(@"PDF Context created");
    
        for (int i = 0 ; i< 10 ; i++)
        {
    
            // page 1
            CGContextBeginPage (pdfContext,nil); 
    
            //turn PDF upsidedown   
            CGAffineTransform transform = CGAffineTransformIdentity;    
            transform = CGAffineTransformMakeTranslation(0, (i+1) * 910);   
            transform = CGAffineTransformScale(transform, 1.0, -1.0);   
            CGContextConcatCTM(pdfContext, transform);  
    
            //Draw view into PDF
            [myScrollView.layer renderInContext:pdfContext];    
            CGContextEndPage (pdfContext);          
            [myScrollView setContentOffset:CGPointMake(0, (i+1) * 910) animated:NO];
    
        }
        CGContextRelease (pdfContext);
    }
    
    - (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path    
    {   
        CGContextRef myOutContext = NULL;   
        CFURLRef url;   
        url = CFURLCreateWithFileSystemPath (NULL, path,                                         
                                             kCFURLPOSIXPathStyle,                                       
                                             false);
    
        if (url != NULL) {      
            myOutContext = CGPDFContextCreateWithURL (url, 
                                                      &inMediaBox,                                                
                                                      NULL);        
            CFRelease(url);     
        }   
        return myOutContext;
    }
    

提交回复
热议问题