在滚动视图中尝试放大图像视图中的png时,我会遇到一些奇怪的行为。我想我已经在我的实用程序窗格中设置了约束或设置错误。如果我想首先看整张照片然后放大它,它们应该是什么?
图像渲染,但是当我尝试捏合放大和缩小时,只有图片的高度变大或变小,宽度卡在iPhone屏幕宽度上。但是,如果我进行了足够的操作,我可以进一步放大,但一切都变得扭曲和旋转。我有什么想法吗?
以下是我现在在View Controller实现文件中的代码:
@interface ZooViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ZooViewController
@synthesize imageView;
@synthesize scrollView;
-(void)viewDidLoad
{
[super viewDidLoad];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)viewDidUnload{
[self setImageView:nil];
[self setScrollView:nil];
[super viewDidUnload];
}
@end
答案 0 :(得分:2)
首先将图像内容模式设置为UIViewContentModeScaleAspectFit
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
之后设置scrollview内容大小
self.scrollView.contentSize = self.areaImageView.frame.size;
不要忘记设置scrollview委托
// For supporting zoom,
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
...
// Implement a single scroll view delegate method
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
return imageView;
}