如何按比例缩放UIImageView
2个回答
推荐于2016-04-18 · 知道合伙人教育行家
关注
展开全部
按比例缩放需要用程序来完成,可以参照下面的程序:
@interface HYShowImageView : UIScrollView <UIScrollViewDelegate>
//显示图像大图
-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect;
@end
-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect
{
_oldRect = rect;
[self setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
UIImageView *showView = [[UIImageView alloc] initWithFrame:_oldRect];
showView.contentMode = UIViewContentModeScaleAspectFit;
[UIView animateWithDuration:0.5f animations:^{
[showView setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
}];
[self setBackgroundColor:color_with_rgba(0, 0, 0, 1)];
[parentsView addSubview:self];
[showView setTag:'show'];
[showView setImage:image]; //这个地方也可以用网络的图片
[self addSubview:showView];
//增加两个手势
showView.userInteractionEnabled = YES;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleViewTap:)];
[self addGestureRecognizer:singleTap];
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchView:)];
[self addGestureRecognizer:pinchGesture];
}
增加了两个手势,一个点击取消,一个缩放
//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
[self setZoomScale:1.0 animated:NO];
[UIView animateWithDuration:0.5f animations:^{
UIImageView *showView = (UIImageView *)[self viewWithTag:'show'];
showView.frame = _oldRect;
self.backgroundColor = color_with_rgba(0, 0, 0, 0.0);
} completion:^(BOOL finished){
[self removeFromSuperview];
}];
}
//缩放图片
-(void)handlePinchView:(UIPinchGestureRecognizer *)sender
{
UIImageView *imageView = (UIImageView *)[self viewWithTag:'show'];
if ([sender state] == UIGestureRecognizerStateBegan) {
_imageHWScale = imageView.image.size.height/imageView.image.size.width;
_beganScale = self.zoomScale;
}
[self setZoomScale:_beganScale * sender.scale];
if ([sender state] == UIGestureRecognizerStateEnded) {
[self scrollViewEnd];
}
}
- (void)scrollViewEnd
{
if (self.zoomScale < 1.0) {
[self setZoomScale:1.0 animated:YES];
self.contentOffset = CGPointMake(0, 0);
} else if (self.zoomScale > 3.0) {
[self setZoomScale:3.0 animated:YES];
}
}
@interface HYShowImageView : UIScrollView <UIScrollViewDelegate>
//显示图像大图
-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect;
@end
-(void)showImage:(UIImage*)image inView:(UIView *)parentsView fromRect:(CGRect)rect
{
_oldRect = rect;
[self setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
UIImageView *showView = [[UIImageView alloc] initWithFrame:_oldRect];
showView.contentMode = UIViewContentModeScaleAspectFit;
[UIView animateWithDuration:0.5f animations:^{
[showView setFrame:CGRectMake(0, 0, PHOTOWIDTH, PHOTOHEIGHT)];
}];
[self setBackgroundColor:color_with_rgba(0, 0, 0, 1)];
[parentsView addSubview:self];
[showView setTag:'show'];
[showView setImage:image]; //这个地方也可以用网络的图片
[self addSubview:showView];
//增加两个手势
showView.userInteractionEnabled = YES;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleViewTap:)];
[self addGestureRecognizer:singleTap];
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchView:)];
[self addGestureRecognizer:pinchGesture];
}
增加了两个手势,一个点击取消,一个缩放
//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
[self setZoomScale:1.0 animated:NO];
[UIView animateWithDuration:0.5f animations:^{
UIImageView *showView = (UIImageView *)[self viewWithTag:'show'];
showView.frame = _oldRect;
self.backgroundColor = color_with_rgba(0, 0, 0, 0.0);
} completion:^(BOOL finished){
[self removeFromSuperview];
}];
}
//缩放图片
-(void)handlePinchView:(UIPinchGestureRecognizer *)sender
{
UIImageView *imageView = (UIImageView *)[self viewWithTag:'show'];
if ([sender state] == UIGestureRecognizerStateBegan) {
_imageHWScale = imageView.image.size.height/imageView.image.size.width;
_beganScale = self.zoomScale;
}
[self setZoomScale:_beganScale * sender.scale];
if ([sender state] == UIGestureRecognizerStateEnded) {
[self scrollViewEnd];
}
}
- (void)scrollViewEnd
{
if (self.zoomScale < 1.0) {
[self setZoomScale:1.0 animated:YES];
self.contentOffset = CGPointMake(0, 0);
} else if (self.zoomScale > 3.0) {
[self setZoomScale:3.0 animated:YES];
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询