我的图像如下:
当我将图像加载到uiimageview然后作为子视图添加到uiscrollview时,在开始时图像显示如下:
问题是我想在开始时看到所有图像都适合屏幕,但它显示已经缩放。有没有办法处理这个请帮助...
我的代码如下:
[self.view addSubview:scrollView];
UIImageView *tempImageView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
答案 0 :(得分:16)
可能你正在寻找这个,
UIImageView *tempImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watson.jpg"]] autorelease];
tempImageView.frame = scrollView.bounds;
self.imageView = tempImageView;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.minimumZoomScale = 1.0 ;
scrollView.maximumZoomScale = imageView.image.size.width / scrollView.frame.size.width;
scrollView.zoomScale = 1.0;
scrollView.delegate = self;
[scrollView addSubview:imageView];
答案 1 :(得分:2)
看一下UIView的contentMode-Property(UIImageView也支持这个)。
imageView.contentMode = UIViewContentModeScaleAspectFit;
这应该有用......
答案 2 :(得分:1)
我认为问题在于
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
Apple的文档称“此方法会调整接收器的帧以匹配指定图像的大小。默认情况下,它还会禁用图像视图的用户交互。”
改为使用
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tempImageView.image = [UIImage imageNamed:@"Tree.jpg"];
答案 3 :(得分:0)
请注意,还有[scrollView zoomToRect:animated:]描述here
您可以将其设置为图像的外边界,以便在视图中自动调整图像。但请确保按照Deepak solution above正确设置maximumZoomScale。
答案 4 :(得分:0)
我使用了Deepak的解决方案和Apples建议,并在其中使用UIImageView子类化了UIScrollView。但是,我只在我的scrollview子类的第一个布局上缩放图像,并且只有当帧大小小于图像大小时才会缩放图像:
- (void)layoutSubviews
{
[super layoutSubviews];
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = _imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
_imageView.frame = frameToCenter;
// If image is bigger than frame, we zoom it on first load (zoom value is smaller than 1.0)
if (_firstLayout) {
_firstLayout = NO;
self.zoomScale = (self.frame.size.width < _imageView.image.size.width) ? (self.frame.size.width / _imageView.image.size.width) : 1.0;
}
}
这是我初始化子类的方式:
- (id)initWithFrame:(CGRect)frame image:(UIImage*)image
{
self = [super initWithFrame:frame];
if (self) {
_firstLayout = YES;
_imageView = [[UIImageView alloc] initWithImage:image];
[self addSubview:_imageView];
self.backgroundColor = [UIColor blackColor];
self.minimumZoomScale = 0.1;
self.maximumZoomScale = 10;
self.delegate = self;
}
return self;
}