Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly replace image in UIImageView in UIScrollView on iOS

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!

like image 609
Borut Tomazin Avatar asked Feb 14 '12 09:02

Borut Tomazin


People also ask

How do I add an image to UIImage?

With Interface Builder it's pretty easy to add and configure a UIImageView. The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project).

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.


2 Answers

There is a property called zoomScale in UIScrollView

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.

like image 98
Ilanchezhian Avatar answered Oct 06 '22 18:10

Ilanchezhian


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:

[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 for centring 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;
}
like image 32
andrei Avatar answered Oct 06 '22 20:10

andrei