Really simple xcode array issue - objective-c

I'm programming a quiz app in xcode and I have 10 images which I want to use as UIImage views. I want to know how to create an array of all those images.
I also have a switch statement where I want to call 1 image from that array to be the UIImage. If you could explain how to call an image from an array as well I would be very grateful :)
Thanks in advance !!

Instead of storing UIImages in the array you can also store image names in array
NSArray *imagesArray = #[#"<image-name-1>",#"<image-name-2>",...,<image-name-10>];
when you want to get the image to set to UIImageView
UIImageView *imageView = [[UIImageView alloc] initwithFrame:<your-frame>];
[imageView setImage:[UIImage imageNamed:imagesArray[<your-index>]];
Let me know if you need any more info.

If you need to call an object from an array, objectAtIndex: can do the job for you. It doesn't matter whether you store UIImage instances of UIImageView instances, this will work with any form of object.
Here's a little example that will call each item from the array using objectAtIndex::
NSArray *_imageViews = ...
for (int idx; idx < _imageViews.count; idx++)
{
UIImageView *_imageView = [_imageViews objectAtIndex:idx];
/* do whatever you have to do with the image view */
}

Related

How to refresh view in objective-c

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];
}
}
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.
try at the start of refresh call
[[self subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
or
for (id imageView in self.subviews){
if([imageView isKindOfClass:[UIImageView class]]) [imageView removeFromSuperview];
}
call [tableview reloadData] if You are using tableview to show your gallery images
or call view's
[self.view setNeedsDisplay] method for refreshing the view.

Variable that refers to many uiimageviews

Okay so what I want to do is I have an app with many uiimageviews that you can move and do actions with them. Instead of creating many outlets for each, how can i create one variable that refers to all these outlets (not IBOutletCollection because i would have to use array and arrays dont have properties that uiimageviews do. Ive tried that if youre positive it works please show me the code). For example I have many uiimageviews, variable = all my uiimageviews
So then [variable move]; it moves all the uiimageviews. (NOT BITWISE & or && OPERATOR)
How about creating all of the UIImageView instances and wrapping them in a single transparent UIView. Then when you want to move all the UIImageViews you'd just need to move their wrapper (said UIView).
update
UIView *wrapper = [[UIView alloc] initWithFrame:bigFrame];
for (size_t i = 0; i<10; i++) {
UIImageView *iv = [[UIImageView alloc] initWithImage:someUIImage];
[wrapper addSubview:iv];
[iv release];
}
[self.view addSubview:wrapper];
[wrapper release];
This code creates an uiview that has 10 uiimageviews inside it. When you move that view (wrapper view) the child image views will move with it.

How can I allocate UIImageView dynamically in iphone sdk?

I want to add number of images from library to a view. The number we want to add to the view is not known. How can I allocate UIImageViews dynamically. And I also want to drag those imageviews on the UIView. Please help me.
Thanks in advance.
Your 'question' consists of way too many parts (the dragging is a different question entirely, one I advice you to look up on Google). And the adding part is easy. The hard part is; where are you getting those images from? Assuming you have an array with image names, it would be something like this;
for( NSString *imageName in (NSArray *)imageNames )
{
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// .. you might want to set a frame to your imageView .. //
[self.view addSubview:imageView];
[imageView release];
}
Of course, this is not what you want, but with the little information provided, it was the best I could do with my spare time.

IOS: fill a NSArray with UIImageView

In my project I created 30 small UIImageView where inside I put a different background. This is my group of UIImageView without background
and this is the group of UIImageView with different background:
I put these group of UImageView inside a UITableViewCell and I use them to define a timeline, but it's not important.
I declared in .h these UIImageView but now I must insert theme in an NSArray, or NSMutableArray? Because in my code (depending on the case) I must set different background for each UIImageView. How can I organize them in my code?
You don't want to put them all in an array. What you actually want to do is assign 'tags' to each view. Then, you can use -[UIView viewWithTag:(int)]; to get a pointer to the image view you want to 'talk' to at the time. Keep in mind not to use negative numbers or 0 when tagging your views.
Although I would suggest drawing your timeline view using a single UIView, dynamically creating and storing views in an array is simple:
NSMutableArray *views = [NSMutableArray array];
NSUInteger viewCount = 30;
NSUInteger index;
for (index = 0; index < viewCount; index++) {
UIImageView *newView = [[[UIImageView alloc] initWithImage:anImage] autorelease];
[newView setFrame:CGRectMake(index * width, 0.0, width, height);
[[self someView] addSubview:newView];
[views addObject:newView];
}
Remember to retain or copy views at some point.
Then, to modify a view:
[[views objectAtIndex:8] setImage:anotherImage];

Loading an image sequence in InterfaceBuilder -ObjectiveC

Using Actionscript and or in the Flash IDE you can either instantiate bitmaps from the library, or simply import bitmaps into the timeline of a MovieClip to create an image sequence.
How would you do the same thing in InterfaceBuilder and-or using ObjectiveC? Do I need to create a new view for each and every image?
You could store them in an array.
Something like so:
NSMutableArray *_imageArray = [NSMutableArray array]; //or you can alloc-init if needed
UIImage *_firstImage = [UIImage imageNamed:#"image1.png"];
UIImage *_secondImage = [UIImage imageNamed:#"rambo.png"];
etc...
[_imageArray addObject:_firstImage];
[_imageArray addObject:_secondImage];
And then go through them in a NSTimer loop or something similar.
Hope it helps