如何缩放UIImageView

时间:2014-08-02 07:47:33

标签: ios uiimageview zooming

我正在CGPDFPage中呈现UIImageView,但如果有任何知道请告诉我,我不会缩放我们如何缩放

        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();

2 个答案:

答案 0 :(得分:0)

您应该将imageView添加为UIScrollView的子视图。

SO answer介绍了如何缩放UIImageView内的UIScrollView

  1. 将视图控制器设置为<UIScrollViewDelegate>
  2. 在视图中心绘制所需尺寸的UIScrollView矩形。将检查器中的最大缩放设置为大于1的值。如4或10。
  3. 右键单击滚动视图并将代理连接到视图控制器。
  4. UIImageView中绘制UIScrollView并将其设置为您想要的任何图像。使其与UIScrollView
  5. 的大小相同
  6. 按住Ctrl键并将UIImageView形式拖动到视图控制器的.h,为IBOutlet创建UIImageView,称之为imageView的聪明之处
  7. 添加此代码:

    -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return self.imageView;
    }
    
  8. 运行应用程序并捏合并平移心脏的内容。

答案 1 :(得分:0)

使用此

- (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

另一种方法是在viewController中实现UITapGestureRecognizer

- (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];

}

基本上这段代码说当在self.view中注册UITabGesture时,tapOnce或tapTwice方法将在self中调用,具体取决于它是单击还是双击。因此,您需要将这些tap方法添加到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];
}

希望有所帮助