Images in array, HOW TO? - ios

I made 26 buttons, A-Z and when I click them the image of the button I pressed will display in an image view, when I click A, it displays A, When I click B it will display B etc.
The problem I have is that they won't display next to each other, I have 6 image views and I want the first letter to fill first box second letter second box etc.
I just don't know how to do this, I've been trying a lot of ways now and i'm pretty sure i'll need to use arrays just can't get the code right.
This is a picture of where I am now I'm already past number 1 so it's like number 2 except for showing next each other ofc.
hope someone can tell me here is the picture:
http://img213.imageshack.us/img213/4500/questionow.png
thanks
This is what I got now .h
IBOutlet UIImageView *imageview1;
IBOutlet UIImageView *imageview2;
IBOutlet UIImageView *imageview3;
IBOutlet UIImageView *imageview4;
IBOutlet UIImageView *imageview5;
IBOutlet UIImageView *imageview6;
}
-(IBAction)showA;
-(IBAction)showB;
-(IBAction)showC;
-(IBAction)showD;
-(IBAction)showE;
-(IBAction)showF;
-(IBAction)showG;
-(IBAction)showH;
-(IBAction)showI;
-(IBAction)showJ;
-(IBAction)showK;
-(IBAction)showL;
-(IBAction)showM;
-(IBAction)showN;
-(IBAction)showO;
-(IBAction)showP;
-(IBAction)showQ;
-(IBAction)showR;
-(IBAction)showS;
-(IBAction)showT;
-(IBAction)showU;
-(IBAction)showV;
-(IBAction)showW;
-(IBAction)showX;
-(IBAction)showY;
-(IBAction)showZ;
.m
-(IBAction)showA {
UIImage *img = [UIImage imageNamed:#"A.png"];
[imageview1 setImage:img];
}
-(IBAction)showB {
UIImage *img = [UIImage imageNamed:#"B.png"];
[imageview1 setImage:img];
}
Etc.
Hope someone can tell me how to make this or has some sample code for me because I don't quite get the arrays even after seeing a lot of tutorials on them.
thanks Don

You could always do:
- (void)setNextImage:(UIImage *)image {
if (imageview1.image == nil) {
[imageView1 setImage:image]
} else if (imageView2.image == nil){
[imageView2 setImage:image]
} // ... Through all 6 images
}
and then in each of your button actions just call:
[self setNextImage:img];
instead of setting it directly.

Using an array is not hard. Just declare either a standard C array of UIImageView* or an NSArray, inited like: [NSArray arrayWithObjects:imageView1, imageView2 ... imageView6, nil].
Then have a position counter pos declared at the instance level, and you simply do:
[imageViewArray objectAtIndex:pos].image = imageParm; pos++;
(Or, for the C array, imageViewArray[pos].image = imageParm; pos++;.)

Related

Selecting image during Animation

I have an outlet called myImage for an imageView that is set up with an animation of 5 images. With a Tap Gesture Recognizer on it, I want to have the users tap on the correct image as it quickly passes to move onto the next level.
There is an error in my code and this does not work:
- (void)viewDidLoad {
[super viewDidLoad];
self.myImage.animationImages =
[NSArray arrayWithObjects:
[UIImage imageNamed:#"examplePic1.png"],
[UIImage imageNamed:#"examplePic2.png"],
[UIImage imageNamed:#"correctPic.png"],
[UIImage imageNamed:#"examplePic4.png"],
[UIImage imageNamed:#"examplePic5.png"],
nil];
[self.myImage setAnimationRepeatCount:0];
self.myImage.animationDuration = 1;
[self.myImage startAnimating];
}
- (IBAction)TapGestureRecognizer:(id)sender {
if (self.myImage.image = [UIImage imageNamed:#"correctPic.png"]) {
// Launch next level
}
else {
// Vibrate Device and subtract points from score
}
}
You have a fundamental misconception in your code about objects and equality. I assume that you meant "==" in your TapGestureRecognizer method. However, even if you changed the = to == would not give you what you want.
The expressions like
[UIImage imageNamed:#"examplePic1.png"]
call a class method of UIImage that creates a new UIImage object. You have created 5 of these objects, put them into an array, an set the animationImage array of your UIImage object to these images. These objects are all different objects from myView.image: I can tell because I can see from your code that you have assigned 5 new objects to the animationObjects array. Your == test cannot ever work because you are comparing self.image to a brand new UIImage object.
What you are trying to do is determine which image the user touched. There is no method in UIImageView that tells you which animation UIImage is currently showing. You are assuming that the image property of the view will be changed as the animation images are displayed, but there is no reason for this assumption to be true: it is certainly not stated in the documentation. Even if you corrected your code (by doing something like this):
if(self.image == [self.animationImages objectAtIndex:2]){
}
your code would not be correct.
If I were trying to do what you want, I would create a UIView with 5 UIImageView as subview, each with their own gesture recognizer. I would use the frame property of each subview to do my animation.
Looks like the options are to determine which image "should" be showing based upon the startTime of the animation.
Determine which of its animationImages a UIImageView is displaying?
Or, just use a timer to set the currently displayed image.
Detect which image was clicked in UIImageView with animationImages

Why can't I access to nested UIView from viewWithTag method using StoryBoard?

I created some UIImageViews inside a nested UIView by Storyboard and I created a different TAG for each UIImageView. This is how the tree of the ViewController looks according to Storyboard:
ViewController
View
ViewNested
UIImageView1
UIImageView1
UIImageView1
UIImageView1
I have to change programmatically these ImageViews so, to get the images I use the method viewWithTag but it doesn't work because it returns NIL.
This happens even if I add to my class the ViewNested IBOutlet and getting the views using the following code:
// View is the top View with Tag:40
UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44
UIView * viewNested = [view viewWithTag:44]; //DOESN'T work it returns NIL even if the TAG is exact
Then if I try to access to the imageView using the same method of course, it returns NIL. I don't know why, I also tried to use this code to view all the recursive nested view but it seems that they don't exist even if they are present in the storyboard.
- (void)showAllSubView:(UIView *)view
{ int i = 0;
for (UIView * subView in [view subviews]) {
NSLog(#"%#, tag:%ld", subView, (long)subView.tag) ;
[self showAllSubView:subView] ;
i++;
}
NSLog(#"Numb of Views %d", i) ;
}
The TAGS are 40 for the root View, 44 for the nested and for the images are 1,2,3,4. So the TAGS are all different.
Any help will be appreciate :). Thanks in advance
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:yourTag];
[imageView setImage:[UIImage imageNamed:#"yourImageName"]];
replace yourTag with UIImageView tag;
replace yourImageName with some Image name
and if you want change only images of ImageViews - you don't need
UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44
UIView * viewNested = [view viewWithTag:44];
Also you can change all images:
for (int i=0; i<18; i++)
{
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:i];
NSString *imageName = [NSString stringWithFormat:#"imageName_%d", i];
[imageView setImage:[UIImage imageNamed:imageName]];
}

Setting an background image over an image set for a UIImageView - Objective C

I am setting images to a UIImage programmatically. But I want to also add a play button over that view. Think of Facebook in how there is a play button over the image. That is exactly what I am trying to figure out on what to do. Here is my setting of the UIImage:
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
Suggestions, thoughts?
You can add playbutton either as a UIButton or a UIImageView and add as a subview to your photoview.
[cell.photoView addSubview:urPlayButtonView];

Displaying image when button clicked

I've build this keyboard and in the picture you can see it, it's the number 1 but I want to it become number 2.
I write with taking the title of every button so button A is titled A and when clicked it will display A in the large label that I have over those empty boxes, the empty boxes are UIview.
Does someone knows in which way to get this working do I have to use different layers? or whatever because I don't know.
hope someone can tell me here is the picture:
http://img213.imageshack.us/img213/4500/questionow.png
thanks
EDIT
This is what I got now .h
IBOutlet UIImageView *imageview1;
IBOutlet UIImageView *imageview2;
IBOutlet UIImageView *imageview3;
IBOutlet UIImageView *imageview4;
IBOutlet UIImageView *imageview5;
IBOutlet UIImageView *imageview6;
}
-(IBAction)showA;
-(IBAction)showB;
-(IBAction)showC;
-(IBAction)showD;
-(IBAction)showE;
-(IBAction)showF;
-(IBAction)showG;
-(IBAction)showH;
-(IBAction)showI;
-(IBAction)showJ;
-(IBAction)showK;
-(IBAction)showL;
-(IBAction)showM;
-(IBAction)showN;
-(IBAction)showO;
-(IBAction)showP;
-(IBAction)showQ;
-(IBAction)showR;
-(IBAction)showS;
-(IBAction)showT;
-(IBAction)showU;
-(IBAction)showV;
-(IBAction)showW;
-(IBAction)showX;
-(IBAction)showY;
-(IBAction)showZ;
.m
-(IBAction)showA {
UIImage *img = [UIImage imageNamed:#"A.png"];
[imageview1 setImage:img];
}
-(IBAction)showB {
UIImage *img = [UIImage imageNamed:#"B.png"];
[imageview1 setImage:img];
}
Etc.
got UIImageview over the empty box and the first letters are shown very nice
Watched some tutorials on mutable arrays but still not sure how to implant it in my code.
Would it be a mutable array with imagevie1, 2,3,4,5,6?
hope you can help me out again
This is more about organizing your thoughts than actually technical stuff.
1) Check how many boxes are filled, so you know what's the next box to be filled.
2) Have a way to access each box.
Edit 1: Building the Data Source
This is just a way to get your started:
1) Start by creating an NSMutableArray.
2) When you click any button, using the size of your NSMutableArray, you can automatically know where the image should be placed:
myArray count = 0 => UIImageView 0 needs to be filled
myArray count = 1 => UIImageView 1 needs to be filled
myArray count = 2 => UIImageView 2 needs to be filled
myArray count = 3 => UIImageView 3 needs to be filled

How can I change the image displayed in a UIImageView programmatically?

I have an IBOutlet to a UIImageView, but when I look at the UIImageView doc, I can't see any hints about programmatically changing it. Do I have to fetch an UIImage object from that UIImageView?
If you have an IBOutlet to a UIImageView already, then all you have to do is grab an image and call setImage on the receiver (UIImageView). Two examples of grabbing an image are below. One from the Web, and one you add to your Resources folder in Xcode.
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
or
UIImage *image = [UIImage imageNamed: #"cell.png"];
Once you have an Image you can then set UIImageView:
[imageView setImage:image];
The line above assumes imageView is your IBOutlet.
That's it! If you want to get fancy you can add the image to an UIView and then add transitions.
P.S. Memory management not included.
Note that the NIB file doesn't wire up all the IBOutlets until the view has been added to the scene. If you're wiring things up manually (which you might be doing if things are in separate NIBs) this is important to keep in mind.
So if my test view controller has an "imageView" wired by a nib, this probably won't work:
testCardViewController.imageView.image = [UIImage imageNamed:#"EmptyCard.png"];
[self.view addSubview:testCardViewController.view];
But this will:
[self.view addSubview:testCardViewController.view];
testCardViewController.imageView.image = [UIImage imageNamed:#"EmptyCard.png"];
This worked for me
[ImageViewName setImage:[UIImage imageNamed: #"ImageName.png"]];
Make sure that the ImageView is declared properly in the .h file and is linked with the IB element.
imageView.image = [UIImage imageNamed:#"myImage.png"];
For the purpose of people who may be googling this to try to solve their problem, remember to properly declare the property in your header file and to synthesize the UIImageView in your implementation file... It'll be tough to set the image programmatically without getter and setter methods.
#import <UIKit/UIKit.h>
#interface YOURCONTROLLERNAME : UIViewController {
IBOutlet UIImageView *imageToDisplay;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;
#end
and then in your .m :
#implementation YOURCONTROLLERNAME
#synthesize imageToDisplay;
//etc, rest of code goes here
From there you should be fine using something like the following to set your image.
[YOURCONTROLLER.imageToDisplay setImage:[UIImage imageNamed:value]];
Example in Swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func myAction(sender: UIButton) {
let newImg: UIImage? = UIImage(named: "profile-picture-name")
self.myUIImageView.image = newImg
}
#IBAction func myAction2(sender: UIButton) {
self.myUIImageView.image = nil
self.myUIImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://url/image.png")!)!)
}
}
Following Jordan's advice (which should work actually), try to set the UIImageView to be visible:
[imageView setHidden: NO];
and also - don't forget to attach it to the main UIView:
[mainView addSubview: imageView];
and to bring to the front:
[mainView bringSubviewToFront: imageView];
Hope combining all these steps will help you solve the mystery.
My problem was that I tried to change the image in an other thread. I did like this:
- (void)changeImage {
backgroundImage.image = [UIImage imageNamed:#"img.png"];
}
Call with:
[self performSelectorOnMainThread : #selector(changeImage) withObject:nil waitUntilDone:NO];
Don't forget to call sizeToFit() after you change image if you then use size of UIImageView to set UIScrollView contentSize and/or compute zoom scale
let image = UIImage(named: "testImage")
imageView.image = image
imageView.sizeToFit()
scrollView.contentSize = imageView.bounds.size
If you want to set image to UIImageView programmatically then Dont Forget to add UIImageView as SubView to the main View.
And also dont forgot to set ImageView Frame.
here is the code
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:#"myImage.png"];
[self.view addSubview:myImage];
myUIImageview.image = UIImage (named:"myImage.png")
Working with Swift 5 (XCode 10.3) it's just
yourImageView.image = UIImage(named: "nameOfTheImage")
UIColor * background = [[UIColor alloc] initWithPatternImage:
[UIImage imageNamed:#"anImage.png"]];
self.view.backgroundColor = background;
[background release];
This question already had a lot of answers. Unfortunately none worked for me.
So for the sake of completenes I add what helped me:
I had multiple images with the same name - so I ordered them in sub folders. And I had the full path to the image file I wanted to show. With a full path imageNamed: (as used in all solutions above) did not work and was the wrong method.
Instead I now use imageWithContentsOfFile: like so:
self.myUIImage.image = [UIImage imageWithContentsOfFile:_currentWord.imageFileName];
Don't know, if anyone reads that far?
If so and this one helped you: please vote up.
;-)
To set image on your imageView use below line of code,
self.imgObj.image=[UIImage imageNamed:#"yourImage.png"];

Resources