views:

374

answers:

0

I wanna take a rect from CGPDFPage (the portion of image around the user's touch point(x,y)) and scale it by a scaleFactor (ie 2x). Below the code I've used to get CGPDFPage's rect. The problem with it is the scaleFactor support. The idea is: 1) pageRect size is pageRect.size *2 2) myThumbRect (the region to zoom) become resultImageSize/scaleFactor (because the final output will be scaleFactor times bigger) 3) pointOfClick (x,y) become pointOfClick(2x,2y) 4) scale up the context by factor CGContextScaleCTM(ctx, scaleFactor, -scaleFactor); 5) grab the rect

However the result is an empty image. Any idea?

-(UIImage *) zoomedPDFImageAtPoint:(CGPoint) pointOfClick size:(CGSize) resultImageSize scale:(CGFloat) scaleFactor {
 // get the rect of our page
 CGRect pageRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
 // my thumb rect is a portion of our CGPDFPage with size as /scaleFactor of resultImageSize
 // then we need to scale the image portiong by *scaleFactor and draw it in our resultImageSize sized graphic context
 CGSize myThumbRect = resultImageSize;
 // page rect has size as original size * scaleFactor

 //resultImageSize = pageRect.size; // to remove, i've used it to see where the rect is printed in final image

 pointOfClick = CGPointMake(-pointOfClick.x, -pointOfClick.y);
 NSLog(@"Click (%0.f,%0.f) Page (%0.f,%0.f ; %0.f,%0.f)",pointOfClick.x,pointOfClick.y,pageRect.origin.x,pageRect.origin.y,pageRect.size.width,pageRect.size.height);

 // create a new context for resulting image of my desidered size
 UIGraphicsBeginImageContext(resultImageSize);

 CGContextRef ctx = UIGraphicsGetCurrentContext();
 CGContextSaveGState(ctx);

 // because rect is that for drawing in a flipped coordinate system, this translate the lower-left corner of the rect
 // in an upright coordinate system
 CGContextTranslateCTM(ctx, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
 // scale to flip the coordinate system so that the y axis goes up the drawing canvas
 CGContextScaleCTM(ctx, 1, -1);
 // translate so the origin is offset by exactly the rect origin
 CGContextTranslateCTM(ctx, -(pageRect.origin.x), -(pageRect.origin.y));


 // zoomRect is interested region.the clickPoint is the center of this region
 CGRect zoomedRect = CGRectMake(-pointOfClick.x, (pageRect.size.height-(-pointOfClick.y)),myThumbRect.width,myThumbRect.height);
 zoomedRect.origin.y-=(myThumbRect.height/2.0);
 zoomedRect.origin.x-=(myThumbRect.width/2.0);
 NSLog(@"Zoom region at (%0.f,%0.f) (%0.f,%0.f)",zoomedRect.origin.x,zoomedRect.origin.y,zoomedRect.size.width,zoomedRect.size.height);

 // now we need to move clipped rect to the origin
 // x: x was moved subtracting current click x coordinate and adding the half of zoomed rect (because zoomedRect contains pointsOfClick at it's center)
 // same with y but inverse (because ctm is flipped)
 CGPoint translateToOrigin = CGPointMake(pointOfClick.x+(zoomedRect.size.width/2.0), -pointOfClick.y-(zoomedRect.size.height/2.0));//(pageRect.size.height-zoomedRect.size.height)+pointOfClick.y);
 NSLog(@"Translate zoomed region to origin using translate by (%0.f,%0.f)",translateToOrigin.x,translateToOrigin.y);
 CGContextTranslateCTM(ctx, translateToOrigin.x,translateToOrigin.y);
 CGContextClipToRect (ctx, zoomedRect);  

 // now draw the document
 CGContextDrawPDFPage(ctx, myPageRef);
 CGContextRestoreGState(ctx);


 // generate image
 UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();  
 UIGraphicsEndImageContext();    
 return finalImage;
}