0

I'm rendering CGPDFPage in UIImageView but not zooming how we can zoom if any know plz let me know

        PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);

        totalPages = (int)CGPDFDocumentGetNumberOfPages(PDFDocument);

        NSLog(@"total pages %i",totalPages);

        //struct CGPDFPage *page =CGPDFDocumentGetPage(PDFDocument, 1);

        CGFloat width = 600.0;

        // Get the page
        CGPDFPageRef myPageRef = CGPDFDocumentGetPage(PDFDocument, i);


        // Changed this line for the line above which is a generic line
        //CGPDFPageRef page = [self getPage:page_number];

        CGRect pageRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFMediaBox);

        CGFloat pdfScale = width/pageRect.size.width;

        pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);

        pageRect.origin = CGPointZero;


        UIGraphicsBeginImageContext(pageRect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        // White BG
        CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
        CGContextFillRect(context,pageRect);

        CGContextSaveGState(context);

        // ***********
        // Next 3 lines makes the rotations so that the page look in the right direction
        // ***********
        CGContextTranslateCTM(context, 0.0, pageRect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFMediaBox, pageRect, 0, true));

        CGContextDrawPDFPage(context, myPageRef);
        CGContextRestoreGState(context);

        imageView= UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
Vishnu
  • 354
  • 3
  • 19
  • http://stackoverflow.com/questions/9008975/how-to-tap-to-zoom-and-double-tap-to-zoom-out-in-ios follow tap gesture and click on image – Shubham Narang Aug 02 '14 at 08:53

2 Answers2

0

You should add your imageView as subview of UIScrollView.

This SO answer describes how to zoom UIImageView inside UIScrollView:

  1. Set your view controller up as a <UIScrollViewDelegate>
  2. Draw your UIScrollView the size you want for the rectangle at the center of the view. Set the max zoom in the inspector to something bigger than 1. Like 4 or 10.
  3. Right click on the scroll view and connect the delegate to your view controller.
  4. Draw your UIImageView in the UIScrollView and set it up with whatever image you want. Make it the same size as the UIScrollView.
  5. Ctrl + drag form you UIImageView to the .h of your View controller to create an IBOutlet for the UIImageView, call it something clever like imageView.
  6. Add this code:

    -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return self.imageView;
    }
    
  7. Run the app and pinch and pan til your heart's content.

Community
  • 1
  • 1
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
  • this method work for me but now how to zoom image in double tap – Monika Patel Sep 06 '15 at 16:50
  • @Stela, you should implement it on your own. Here is exaple from Apple's documentation: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomingByTouch/ZoomingByTouch.html. Or also you can use gesture recognizer. – Sviatoslav Yakymiv Sep 07 '15 at 18:57
  • already i have put this method and its work .. i have problem with double tap zoom – Monika Patel Sep 08 '15 at 02:46
-1

Go with this

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.imageView;

}
- (void)viewDidLoad {

    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;

}

Check Apple Documentation

Another way is to implement UITapGestureRecognizer in your viewController

- (void)viewDidLoad
{
    [super viewDidLoad];       

    // target - what object is going to handle 
    // the gesture when it gets recognised
    // the argument for tap: is the gesture that caused this message to be sent
    UITapGestureRecognizer *tapOnce = 
     [[UITapGestureRecognizer alloc] initWithTarget:self  
                                             action:@selector(tapOnce:)];
    UITapGestureRecognizer *tapTwice = 
     [[UITapGestureRecognizer alloc] initWithTarget:self  
                                             action:@selector(tapTwice:)];

    tapOnce.numberOfTapsRequired = 1;
    tapTwice.numberOfTapsRequired = 2;

    //stops tapOnce from overriding tapTwice
    [tapOnce requireGestureRecognizerToFail:tapTwice];

    // then need to add the gesture recogniser to a view 
    // - this will be the view that recognises the gesture  
    [self.view addGestureRecognizer:tapOnce];
    [self.view addGestureRecognizer:tapTwice];

}

Basically this code is saying that when a UITabGesture is registered in self.view the method tapOnce or tapTwice will be called in self depending on if its a single or double tap. You therefore need to add these tap methods to your UIViewController:

- (void)tapOnce:(UIGestureRecognizer *)gesture
{
    //on a single  tap, call zoomToRect in UIScrollView
    [self.myScrollView zoomToRect:rectToZoomInTo animated:NO];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
    //on a double tap, call zoomToRect in UIScrollView
    [self.myScrollView zoomToRect:rectToZoomOutTo animated:NO];
}

Hope that helps

Nazim
  • 64
  • 3