title: How to zoom only visible image in image view not transparent part of image?
body: <p>I have a scrollView in a UIView and a imageView in the ScrollView.scrollview and imageview have same size.I have set image view content mode is UIViewContentModeScaleAspectFit . when i double tap on image view , image view not zoom only visible image of image view but also zoom transparent portion of image.</p>
  
  <p>I want only zoom visible image of image view.</p>
  
  <pre><code>image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screen_width,550)];
  self.image_view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, image_scroll.frame.size.width, image_scroll.frame.size.height)];
  self.image_view.backgroundColor=[UIColor clearColor];
  self.image_view.contentMode = UIViewContentModeScaleAspectFit;
  self.image_view.clipsToBounds = true;
  self.image_view.userInteractionEnabled = YES;
  self.image_view.image=[UIImage imageNamed:@"img1.png"];
  doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  [doubleTap setNumberOfTapsRequired:2];
  [self.image_view addGestureRecognizer:doubleTap];
  [image_scroll setMaximumZoomScale:4.0];
  [image_scroll addSubview:self.image_view];
  
  
  - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
  
          NSLog(@"handleDoubleTap");
  
          float newScale = [image_scroll zoomScale] * 2.0;
  
          if (newScale &gt; image_scroll.maximumZoomScale){
              newScale = image_scroll.minimumZoomScale;
              CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
  
              [image_scroll zoomToRect:zoomRect animated:YES];
  
          }
          else{
  
              newScale = imageScroll.maximumZoomScale;
              CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
  
              [image_scroll zoomToRect:zoomRect animated:YES];
          }
      }
  }
  
  
  - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
  
      CGRect zoomRect;
      // the zoom rect is in the content view's coordinates.
      //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
      //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
      zoomRect.size.height = [image_scroll frame].size.height / scale;
      zoomRect.size.width  = [image_scroll frame].size.width  / scale;
  
      // choose an origin so as to get the right center.
      zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
      zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
  
      return zoomRect;
  
  }
  </code></pre>
  
  <p>1.Original image
  <a href="https://i.stack.imgur.com/Jk7CP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jk7CP.png" alt="enter image description here"></a>
  2.After image zoom
  <a href="https://i.stack.imgur.com/AhcxW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AhcxW.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/DTiul.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DTiul.png" alt="enter image description here"></a></p>
  
  <p>Thanks in advance.</p>