Note that there are some explanatory texts on larger screens.

plurals
  1. POreseting CGContextRef after drawing pdf page using CGContextDrawPDFPage
    primarykey
    data
    text
    <p>I am trying to create thumb images for every pdf page in PDF document and place it in a UISCrollVIew. I have succeeded in this, but scrolling is not so smooth as I want when it's too fast. And I want to optimize thumb images creating for Pdf page. I want to create one CGContextRef and reset its content after CGContextDrawPDFPage, as a consequence I wouldn't have to create a context each time and perform some other calculation, which takes a lot of resources.</p> <p>Is it possible to reset CGContextRef content after CGContextDrawPDFPage? CGContextRestoreGState and CGContextSaveGState seems to doesn't help in this situation.</p> <pre><code>NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; GCPdfSource *pdfSource = [GCPdfSource sharedInstance]; for (int i = pageRange.location; i &lt;= pageRange.length; i++) { UIView *thumbPdfView = [scrollView viewWithTag:i+1]; if (thumbPdfView == nil) { CGPDFPageRef pdfPage = [pdfSource pageAt:i + 1]; float xPosition = THUMB_H_PADDING + THUMB_H_PADDING * i + THUMB_WIDTH * i; CGRect frame = CGRectMake(xPosition, THUMB_H_PADDING, THUMB_WIDTH, THUMB_HEIGHT); thumbPdfView = [[UIView alloc] initWithFrame:frame]; thumbPdfView.opaque = YES; thumbPdfView.backgroundColor = [UIColor whiteColor]; [thumbPdfView setTag:i+1]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, frame.size.width, frame.size.height, 8, /* bits per component*/ frame.size.width * 4, /* bytes per row */ colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextClipToRect(context, CGRectMake(0, 0, frame.size.width,frame.size.height)); CGRect pdfPageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); CGRect contextRect = CGContextGetClipBoundingBox(context); CGAffineTransform transform = aspectFit(pdfPageRect, contextRect); CGContextConcatCTM(context, transform); CGContextDrawPDFPage(context, pdfPage); CGImageRef image = CGBitmapContextCreateImage(context); CGContextRelease(context); UIImage *uiImage = [[UIImage alloc]initWithCGImage:image]; CGImageRelease(image); UIImageView *imageVIew = [[UIImageView alloc]initWithImage:uiImage]; [uiImage release]; [thumbPdfView addSubview:imageVIew]; [imageVIew release]; [scrollView addSubview:thumbPdfView]; [thumbPdfView release]; } } [pool release];//release </code></pre> <p>and aspectFit function...</p> <pre><code>CGAffineTransform aspectFit(CGRect innerRect, CGRect outerRect) { CGFloat scaleFactor = MIN(outerRect.size.width/innerRect.size.width, outerRect.size.height/innerRect.size.height); CGAffineTransform scale = CGAffineTransformMakeScale(scaleFactor, scaleFactor); CGRect scaledInnerRect = CGRectApplyAffineTransform(innerRect, scale); CGAffineTransform translation = CGAffineTransformMakeTranslation((outerRect.size.width - scaledInnerRect.size.width) / 2 - scaledInnerRect.origin.x, (outerRect.size.height - scaledInnerRect.size.height) / 2 - scaledInnerRect.origin.y); return CGAffineTransformConcat(scale, translation); </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload