How to assign something to a variably named UIImageView? - ios

I have 5 objects that i want to assign to UIImageViews named "obstacle1","obstacle2",etc. how can i do this in a for loop somewhat like this...
for(int i=0;i<5;i++)
{
UIImageView "obstacle%d",i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square.png"]];
}
//the <"obstacle%d",i> part is what i need help with

You should really use an Array for holding your UIImageView objects:
NSMutableArray *imageViews = [NSMutableArray array];
for(int i=0;i<5;i++)
{
UIImageView *anObstacle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"square%d.png",colorPick]]];
[imageViews addObject:anObstacle];
}
Then you can just access them in the array by doing imageViews[0].

Related

i want show different images continuously without user touch the button

when the user touch the button the below code show the image.i want show different images like "bear,cat,cow,dog" continuously without user touch the button.can any one help me.
NSMutableArray *dashBoy = [NSMutableArray array];
for (i = 1; i<= 12; i++)
{
butterfly = [NSString stringWithFormat:#"bear_%d.png", i];
if ((image = [UIImage imageNamed:butterfly]))
[dashBoy addObject:image];
}
[stgImageView setAnimationImages:dashBoy];
[stgImageView setAnimationDuration:4.0f];
[stgImageView setAnimationRepeatCount:2];
[stgImageView startAnimating];
You could do as below:-
Firstly, create a UIImageView property like below,
#property (nonatomic,weak) IBOutlet UIImageView *imageView; //Also connect it with storyboard or xib.
Then you could write down IBAction as follow:-
- (IBAction)startTap:(id)sender {
[self.imageView setAnimationDuration:1.0f]; //You could change the duration for making animation fast or slow.
[self.imageView setAnimationImages:imageArray]; //imageArray is NSArray of images of which you want to show slide show.
[self.imageView startAnimating]; //simply to start animation.
}
- (IBAction)stopTap:(id)sender {
[self.imageView stopAnimating]; //To stop animation.
}
try this..
create two instance objects like
NSMutableArray * imagesArray; and UIImageView *animationImageView;
-(void)viewDidLoad
{
NSArray *imageNames = #[#"img1.png", #"img2.png", #"img3.png", #"img4.png",#"img5.png"];
imagesArray = [[NSMutableArray alloc]init];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[imagesArray addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = imagesArray;
animationImageView.animationDuration = 0.5; // set your own
[self.view addSubview:animationImageView];
}
and to start and stop two UIButton Actions
-(IBAction)startAnimation:(UIButton*)sender
{
[animationImageView startAnimating];
}
-(IBAction)stopAnimation:(UIButton*)sender
{
[animationImageView stopAnimating];
}

-(void) loadView with NSMutableArray

- (void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:#"11.jpg",#"22.jpg",#"33.jpg", nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
}
When I put the NSMutableArray in -(void)viewDidLoad, UIImageView displays nothing and NSLog shows NULL. Why is that?
ps. NSMutableArray worked perfectly in -(void)loadView. I've declared NSMutableArray *arrayOfImage in #interface .h file
The only way that the given could would output "NULL" in this situation is that arrayOfImages is NULL by itself.
This is only possible if arrayOfImages is declared as a weak variable.
But as #maddy pointed out, your code is all wrong: Don't call super, but assign self.view. Or use viedDidLoad instead (probably what you want here).
-(void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"11.jpg"],[UIImage imageNamed:#"22.jpg"],[UIImage imageNamed:#"33.jpg"], nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
This will work but you will only get to see 1 picture. ie the picture in the 0 index of the array.
If you want to show all then you got to apply a loop.

xcode finding contents of an UIImageview

I have created an array of images and they start in an imageview. The user can pick up the images using a gesture and drag and drop them in a separate imageview. They can move them about back and forth and they shuffle and organise themselves and all is well.
But I have no idea which ones they have moved into which imageview. How do I identify which image lives in which imageview. Can I query what is in an imageview?
I looked at tags and that didn't help much.
I generate array as so
NSArray *pngs = [NSArray arrayWithObjects:#"red", #"blue", #"green", #"yellow", #"purple", #"orange", #"black", #"white", nil];
for (NSString *png in pngs) {
UIImage *draggableImage = UIImageWithBundlePNG(png);
UIImageView *draggableImageView = [[UIImageView alloc] initWithImage: draggableImage];
SEDraggable *draggable = [[SEDraggable alloc] initWithImageView: draggableImageView];
[self configureDraggableObject: draggable];
Not sure where to even start.
try adding tag to UIImageView
NSArray *pngs = [NSArray arrayWithObjects:#"red", #"blue", #"green",
#"yellow", #"purple", #"orange", #"black", #"white", nil];
for (NSString *png in pngs) {
UIImage *draggableImage = UIImageWithBundlePNG(png);
UIImageView *draggableImageView = [[UIImageView alloc] initWithImage: draggableImage];
draggableImageView.tag = i++;//i must be initialized
SEDraggable *draggable = [[SEDraggable alloc] initWithImageView: draggableImageView];
[self configureDraggableObject: draggable];
you can identify the imageview by [imageView viewWithTag:TAG]; method.
To know the the view when a view has been attached you need just to look the property 'superView' so in your case if want to know the view that has inside the UIImageView, you need to have a reference to the UIImageView like:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bla"]];
UIView *imageContainer = imageView.superView;
if you assign a tag to your container now you are able to do that:
if (imageContainer.tag == aTag) {
Do something...
}

Can't access UIImageView if created programmatically

If I create a UIImageView via Storyboard and add it as a #property into a ViewController.h, I can access that UIImageView from anywhere in the ViewController.m via [self UIImageView] or self.UIImageView.
But If I create the UIImageView programmatically from viewDidLoad as you see below, I seem to lose the ability to access it from outside viewDidLoad.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
So later on if I want to modify the image that is being displayed in any one of the UIImageView I've created from viewDidLoad, I can't seem to access it. Does anyone know what I'm doing wrong?
You would need to create your UIImageView in your header file and then it would be available throughout the rest of the view. You will need to add it to the view in viewDidLoad though.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
The answer AgnosticDev is trying to say here is to add an ivar to your view controller's .h file... e.g.:
#implementation YHLViewController : ViewController
{
UIImageView * currImageView;
}
And then in your "viewDidLoad" method, you can allocate and initialize it via:
currImageView = [[UIImageView alloc] init];
and have access to it from anywhere else in your view controller.

Loading images into a UIScrollView

I'm hoping somebody can help me out here, I'm not sure what else to do. I have a UIScrollView that loads a set of 44 images, and then allows the user to page through them. My code was working, then suddenly it stopped. I have no idea what I have changed so any help would be much appreciated:
The previous view is a UITableView, it passes a string along with the name of the cell that was selected. Here is the ViewDidLoad Method from the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Answer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(answerPressed:)];
self.navigationItem.rightBarButtonItem = rightButton;
if([questionSetName isEqualToString:#"Limitations"]){
[self limitationsView];
}
}
Here is the limitationsView:
-(void)limitationsView{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Images/Limitations"];
NSMutableArray *limitationImages = [[NSMutableArray alloc] init];
for (int x = 0; x<[unsortedArray count]; x++) {
NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];
[limitationImages insertObject:fileName atIndex:x];
}
NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
context:(__bridge void *)([NSLocale currentLocale])];
for(int i = 0; i< [sortedArray count]; i++){
CGFloat xOrigin = i * self.view.bounds.size.width;
UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;
[limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];
limitationImageView.contentMode = UIViewContentModeScaleAspectFit;
[scroll addSubview:limitationImageView];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);
[self.view addSubview:scroll];
}
When I get to the end of the LimitationsView method the sortedArray contains all 44 images, and the proper names, if I try and set the background color of the scrollview I can successfully do that. but my images are not loading and I am at a loss as to why?
I would suggest using
NSLog(#"image:%#",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]);
to see if the image is null at that point. If that's the case, then there is a problem with the strings you are storing in that array i.e., they are not identical to the actual names of your images

Resources