0

I call this method in my ViewController, which contains a UIScrollView, when the user taps a button.

-(void) updateView:(NSURL *) urlPicture{
      UIImageView *imageNews = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,200)];
    imageNews.contentMode = UIViewContentModeScaleAspectFit;
    [imageNews setImageWithURL:urlPicture];
    [self.scrollView addSubview:imageNews];
}

It seems to work fine, but when I got a strange behavior. It doesn't clear the previous image to put the new one. E.g: if the first image is 300x200 and the second one is 200x200 I still can see the sides of the first one when the app download the second.

I would like to know how to clear the previous image before download the second. I already try imageNews.image = nil; but it didn't work

dandan78
  • 13,328
  • 13
  • 64
  • 78
user3249186
  • 185
  • 4
  • 13

2 Answers2

1

First you need to remove old view from scrollView. You can edit your code as mentioned below

#define IMAGE_VIEW_TAG     1001
-(void) updateView:(NSURL *) urlPicture{
    UIView *oldView = [self.scrollView viewWithTag:IMAGE_VIEW_TAG];
    if(oldView)
        [oldView removeFromSuperView]
    UIImageView *imageNews = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,200)];
    imageNews.tag = IMAGE_VIEW_TAG;
    imageNews.contentMode = UIViewContentModeScaleAspectFit;
    [imageNews setImageWithURL:urlPicture];
    [self.scrollView addSubview:imageNews];
}
kkumpavat
  • 452
  • 2
  • 10
0

Well I am not really sure what your application does , but seems like you need to have only 1 image so I would suggest you to remove the previous image.

There are 2 ways to do so:

1) you could use your image as property and simply call [_imageNews removeFromSuperView];

2) you can iterate through your self.scrollView's sub views and remove it from there.

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
  • I have a Array with URLs and when the user click "Next" it shows the next picture. Something like that. I think the problem is that this method doesn't actually do what I want. I want to "destroy" the UIViewController and call it again with other parameters. Something like destroy and create Fragments in Android. There is a way to do that? – user3249186 Jun 08 '14 at 17:41
  • Better just do what I suggested or the answer bellow is actually did it for you which is basically same . – Coldsteel48 Jun 08 '14 at 17:42