我有一个有趣的问题。我有一个 UIScrollView ,里面只有一个带有图像的 UIImageView 。我需要这个来缩放和平移。
好的。在触摸按钮时,我想用下一个图像替换 UIImageView 中的图像(如前面提到的,它位于 UIScrollView 中)。
到目前为止,我已经这样做了:
self.imageView = nil;
self.imageView = [[UIImageView alloc] initWithImage:nextImage];
我总是将 self.imageView 设置为 nil 的原因是,如果我不这样做,那么下一张图像会像前一张图像一样缩放或缩放。
但我不认为这对于内存效率来说是一个很好的解决方案。
有没有其他方法可以通过“重置”选项在 UIImageView 上设置新图像以进行缩放、缩放等...?
更新:
仍然不起作用的代码:
[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.imageView setFrame:rect];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = [self.scrollView frame].size.width / [self.imageView frame].size.width;
if (minZoomScale < [self.scrollView frame].size.height / [self.imageView frame].size.height) {
minZoomScale = [self.scrollView frame].size.height / [self.imageView frame].size.height;
}
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];
更新2
刚刚弄清楚我做错了什么。我没有重置最小缩放比例。
[self.scrollView setMinimumZoomScale:1.0];
现在可以了!
I have an interesting problem. I have one UIScrollView and inside only one UIImageView with image. I need this for zooming and panning.
Ok. On a button touch I want to replace image within UIImageView (which is within UIScrollView, like a mentioned) with the next one in line.
So far I've done this like that:
self.imageView = nil;
self.imageView = [[UIImageView alloc] initWithImage:nextImage];
The reason that I always set self.imageView
to nil is that if I don't do that than the next image is scaled or zoomed like the previous one was.
But I don't think thats fine solution for memory efficiency.
Is there any other way to set new image on UIImageView with "reset" option for zooming, scale, etc... ?
UPDATE:
The code that still does not work:
[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.imageView setFrame:rect];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = [self.scrollView frame].size.width / [self.imageView frame].size.width;
if (minZoomScale < [self.scrollView frame].size.height / [self.imageView frame].size.height) {
minZoomScale = [self.scrollView frame].size.height / [self.imageView frame].size.height;
}
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];
UPDATE 2
Just figured out what I was doing wrong. I did not reset the minimum zoom scale.
[self.scrollView setMinimumZoomScale:1.0];
Now it works!
发布评论
评论(4)
zoomScale 的属性"noreferrer">
UIScrollView
将 1.0 设置为滚动视图的 ZoomScale。无需每次释放旧的 UIImageView 并分配新的 UIImageView。您可以直接将图像设置为imageview,并将UIScrollView的
zoomScale
设置为1.0。There is a property called
zoomScale
inUIScrollView
Set 1.0 to the zoomScale of your scrollview. No need to release the old UIImageView and to allocate a new UIImageView everytime. You can directly set the image to the imageview and set the
zoomScale
of the UIScrollView to 1.0.只是想澄清一件事,因为我遇到了同样的问题:在更改 imageView 的图像之前将最小缩放比例重置为 1.0:
以及将图像居中:
Just want to make one thing clear because I ran into the same problem: reset the minimum zoom scale to 1.0 BEFORE changing the image of the imageView:
and for centring the image:
您只需使用scrollView对象的zoomScale属性并将其设置为1.0每次更改图像。这可能会在加载新图像之前进行。这样,每次您只想更改图像时,就不必分配并初始化一个新的 UIIMageVIew 对象。另外,您可以将此更改放入动画块中,使图像变化不是尖锐而是渐进的。
You can just use the zoomScale property of the scrollView object and set it to 1.0 everytime change the image. This'll probably go before loading the new image. This way you'll not have to alloc and init a new UIIMageVIew object each time you want to change just the image. Additionally you can put this change into an animation block to make the image change not sharp but gradual.
为什么你使用这段代码,每次创建新对象时,但以前的 UIImageView 保留在你的 UIScrollView 上。这对于内存使用效率不高。
使用下一个代码:
或者
然后,您可以为 ScrollView 设置内容大小:
Whey you use this code, every time you create new object, but previous UIImageView stay on your UIScrollView. This is not efficient in memory usage.
Use next code:
or
and then, you can set content size for you ScrollView: