in Education by
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]; } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    How we can layout subviews in a view in Objective-C?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    Why create a custom view in Objective-C?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    I want to get variable value from an objective-c class, using reflection. company.h looks like @interface ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    How do I call Objective-C code from Swift?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    Can we create Dynamic Classes in Objective C? If Yes, Explain how to create with a valid use case?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    Explain how to call function in Objective-C....
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    My objective-c app needs to be aware if the firewall in OSX is running, so it can tell the user ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    Using iOS 5's new TWRequest API, I've ran into a brick wall related with block usage. What I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I'm still trying to understand this piece of code that I found in a project I'm working on where ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I'm still trying to understand this piece of code that I found in a project I'm working on where ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm making a 'post text' app and trying to update my database with name and body field. The obj-c code looks like this: ... (@"%@", strResult); } And the php code looks like this:...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Can we use both swift (.swft) and objective c (.h .m) in same xcode project ?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    Explain the difference between Swift vs Objective-C.?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    What are benefits of collections views in Objective-C?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    What are the size classes in Objective-c?...
asked Nov 10, 2020 in Technology by JackTerrance
...