如何在图像视图中仅缩放可见图像而不是图像的透明部分?
Posted
技术标签:
【中文标题】如何在图像视图中仅缩放可见图像而不是图像的透明部分?【英文标题】:How to zoom only visible image in image view not transparent part of image? 【发布时间】:2016-09-12 11:48:30 【问题描述】:我在 UIView 中有一个 scrollView,在 ScrollView.scrollview 和 imageview 中有一个 imageView 具有相同的大小。我已将图像视图内容模式设置为 UIViewContentModeScaleAspectFit 。当我双击图像视图时,图像视图不仅缩放图像视图的可见图像,还缩放图像的透明部分。
我只想缩放图像视图的可见图像。
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 > 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;
1.原图 2.图片缩放后
提前致谢。
【问题讨论】:
我也遇到了这个问题。 【参考方案1】:目标-C
我认为您应该检查我的演示项目: https://github.com/VivekVithlani/PinchZoom-Objective-c.git
我没有实现双击缩放,它只是捏缩放。
您的问题已解决 100%
【讨论】:
以上是关于如何在图像视图中仅缩放可见图像而不是图像的透明部分?的主要内容,如果未能解决你的问题,请参考以下文章