How to properly replace image in UIImageView in UIScrollView on iOS

I have an interesting problem. I have one UIScrollView and inside there is only one UIImageView with an image. I need this for zooming and panning. OK. When I click the button, I want to replace the image in the UIImageView (which is in the UIScrollView, like the one mentioned) with the next line.

So far I have done this:

self.imageView = nil; self.imageView = [[UIImageView alloc] initWithImage:nextImage]; 

The reason that I always set self.imageView to nil is because if I do not do this, then the next scaling is scaled or scaled like the previous one.

But I do not think this is a great solution for memory efficiency.

Is there any other way to set a new image in UIImageView with the β€œreset” option for scaling, scaling, etc. ??

UPDATE: Code that still doesn't 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 realized what I'm doing wrong. I did not reset the minimum scale scale.

 [self.scrollView setMinimumZoomScale:1.0]; 

Now it works!

+7
source share
4 answers

In UIScrollView

Set 1.0 to zoomScale of your scroll. No need to release the old UIImageView and select the new UIImageView each time. You can directly set image to image and set zoomScale UIScrollView to 1.0.

+7
source

You can simply use the zoomScale property of the scrollView object and change it to 1.0 each time. This will probably happen before uploading a new image. This way, you don’t have to select and run a new UIIMageVIew object every time you want to change only the image. In addition, you can put this change in the animation block to change the image not sharp, but gradual.

+1
source

I just want to make one thing understandable, because I faced the same problem: reset the minimum zoom scale to 1.0 BEFORE replacing the imageView image:

 [self.scrollView setMinimumZoomScale:1.0]; [self.scrollView setZoomScale:1.0]; [self.imageView setImage:photoImage]; [self.photoView setFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), photoImage.size=photo.size}]; [self.scrollView setContentSize:[self.imageView frame].size]; CGFloat minZoomScale = MIN([self.scrollView frame].size.width / [self.imageView frame].size.width, [self.scrollView frame].size.height / [self.imageView frame].size.height) [self.scrollView setMinimumZoomScale:minZoomScale]; [self.scrollView setMaximumZoomScale:1.0]; [self.scrollView setZoomScale:[self.scrollView minimumZoomScale]]; [self centerScrollViewContents]; 

and to center the image:

 - (void)centerScrollViewContents { CGSize boundsSize = self.scrollView.bounds.size; CGRect contentsFrame = self.imageView.frame; if (contentsFrame.size.width < boundsSize.width) { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f; } else { contentsFrame.origin.x = 0.0f; } if (contentsFrame.size.height < boundsSize.height) { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f; } else { contentsFrame.origin.y = 0.0f; } self.imageView.frame = contentsFrame; } 
+1
source

Whey you use this code every time you create a new object, but the previous UIImageView remains on your UIScrollView. It is not effective when using memory.

Use the following code:

 [self.imageView removeFromSuperview]; [self.imageView release]; self.imageView = [[UIImageView alloc] initWithImage: nextImage]; [self addSubview: imageView]; 

or

 [self.imageView setImage: nextImage]; [self.imageView setFrame: CGRectMake(0, 0, nextImage.size.width, nextImage.size.height)]; 

and then you can set the ScrollView content size for it:

 topScrollView.contentSize = CGSizeMake(nextImage.size.width, nextImage.size.height); 
0
source

All Articles