Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:42:37+00:00 2026-05-27T13:42:37+00:00

I wanted to create a gallery. It loads different images based on the category,

  • 0

I wanted to create a gallery. It loads different images based on the category, that a user selects. I used to populate images in UIImageViews.

My when selecting different categories is that it does not clear the previously selected images. This is my code for populating images.

-(void)refresh{

   categoryLabel.text = treatment.treatmentName;
   NSMutableArray  *galleryImages =[NSMutableArray alloc] ;
   galleryImages = [[PatientImage alloc]find:treatment.treatmentId];

   int imgCount = [galleryImages count];

    for(int i=0;i<imgCount;i++){

        PatientImage *apatientImage = [galleryImages objectAtIndex:i]; 

        UIImage *img1 = [UIImage imageNamed:apatientImage.imageBefore]; 
        UIImageView *myImageView = [[UIImageView alloc] initWithImage:img1];
        myImageView.contentMode = UIViewContentModeTopRight;
        myImageView.frame = CGRectMake(120+i*240,120.0,100.0, 100.0);
       [self.view addSubview:myImageView];

        UIImage *img2 = [UIImage imageNamed:apatientImage.imageAfter]; 
        UIImageView *myImageView2 = [[UIImageView alloc] initWithImage:img2];
        myImageView2.contentMode = UIViewContentModeTopRight;
        myImageView2.frame = CGRectMake(120+i*240+300,120.0,100.0, 100.0);
        [self.view addSubview:myImageView2];
    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:42:38+00:00Added an answer on May 27, 2026 at 1:42 pm

    First things first, You have some memory leaks there. You are allocating UIImageViews but are not releasing them anywhere, after you have added them to your view. I don’t know if that applies to ARC, though. Same applies to your Mutable array, but I suppose you are releasing it after the ‘for’ loop somewhere, since it seems you posted code after omitting some of it.

    As far as your actual question is concerned, I wouldn’t do this this way. I would make the mutable array an object variable, and then fill it with my image views. When calling refresh again, I would first call -removeFromSuperview on each image view, then empty the array, then repopulate it and add the new subviews to my view. That is the simple way.

    I don’t know if you are using ARC, but you should be careful about memory management when using dynamically loaded views. Each time you add a view to another one, you increase its retain counter. You must then call release to remove ownership, and let the iOS runtime handle the rest.

    Also note that operations such as this using views are expensive in terms of memory. So, another way of repopulating the gallery view is to just change the image an imageView holds. That will save you some memory, and time. In case the view doesn’t have a constant number of images to be displayed, you can refine your algorithm to change the images on the already created image views, and then add more image views if necessary or delete the remaining ones, if any.

    I hope I helped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create smth similar to this one Kansas county map where user
I wanted to create a very simple method that switches between views in a
I wanted to create a text based browser game, so how should I go
I wanted to create a new WordPress page that is actually a link to
I wanted to create a WCF that employs a singleton pattern but the service
I wanted to create a 3 level dijit.Tree , like that: -root | --level1
I wanted to create a button that had an image and a textblock as
I wanted to create a view that looked something like the following but i
I wanted to create a drop down at a certain position based on a
I wanted to create an invitation form for user to signup. But the signup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.