Image Array IBAction to view next image in the array - iphone

Thanks to #JFoulkes I have got my app to show the first image in the array when clicking the next button. However, when I click it again, it does not show the next image in the array. This leads me to believe my IBAction for the next button is not quite right.
Here's the code I have so far...
In my .h file I have declared:
IBOutlet UIImageView *imageView;
NSArray *imageArray;
NSInteger currentImage;
And I also added:
-(IBAction) next;
In the .m file I have:
-(void) viewDidLoad;
{
imageArray = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil] retain];
}
This is my IBAction which is only ever showing the first image in the array (1.png) and when clicked again doesn't change the UIImageView to the second array image (2.png):
-(IBAction)next {
if (currentImage + 1 == [imageArray count])
currentImage = 0;
UIImage *img = [imageArray objectAtIndex:currentImage];
[imageView setImage:img];
currentImage++;
}
Based on the code, how can I change the IBAction to successully iterate through the images in the array after ever click?

Your incrementing logic is broken, so the first click will do nothing and you can never reach the last image. If you would add a 3.png to your array, this would be a little more obvious. It might be instructive to step through the code, watching what it does at each step.
Correct incrementing logic would look something like this:
- (void)next {
currentImage++;
if (currentImage >= imageArray.count) currentImage = 0;
UIImage *img = [imageArray objectAtIndex:currentImage];
[imageView setImage:img];
}

You need to remove
currentImage = 0;
This means the first image will always be loaded
You need to add a check to see if currentImage is larger than the imagearray:
if (currentImage +1 < [imageArray count])
{
currentImage++;
UIImage *img = [imageArray objectAtIndex:currentImage];
[imageView setImage:img];
}

Create an array of images name it as images, initialize an integer "i".
In the view, create an imageView and two buttons(next and previous),
Use this code for the button action,
-(void)nextButton:(id)sender
{
if (i < (array.count-1))
{
i=i+1;
imageView.image = [images objectAtIndex:i];
}
}
-(void)previousButton:(id)sender
{
if(i >= 1)
{
i=i-1;
imageView.image=[images objectAtIndex:i];
}
}

Related

Not reloading imageview

I have an 20 buttons that each an every button clicked load that array image with respect to their tag value. my problem is that only display first button image why this happning i tried this below code.
images=[[NSMutableArray alloc]init];
[images insertObject:#"circle.png" atIndex:0];
[images insertObject:#"cone.png" atIndex:1];
[images insertObject:#"cube.png" atIndex:2];
[images insertObject:#"cuboid.png" atIndex:3];
[images insertObject:#"cylinder.png" atIndex:4];
[images insertObject:#"ecplise.png" atIndex:5];
[images insertObject:#"ellipsoid.png" atIndex:6];
[images insertObject:#"frustrum of cone.png" atIndex:7];
[images insertObject:#"kite.png" atIndex:8];
[images insertObject:#"parallelepiped.png" atIndex:9];
[images insertObject:#"parallelogram.png" atIndex:10];
[images insertObject:#"polygon.png" atIndex:11];
[images insertObject:#"rectangle.png" atIndex:12];
[images insertObject:#"rectangula prizm.png" atIndex:13];
[images insertObject:#"rhoumbus.png" atIndex:14];
[images insertObject:#"sector.png" atIndex:15];
[images insertObject:#"sphere.png" atIndex:16];
[images insertObject:#"square.png" atIndex:17];
[images insertObject:#"tapezoid.png" atIndex:18];
[images insertObject:#"tourus.png" atIndex:19];
[images insertObject:#"traingle.png" atIndex:20];
NSString * str=value2;
NSLog(#"%#",str);
//AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSInteger myInt = [str intValue];
NSString * imagename=[images objectAtIndex:myInt];
NSLog(#"%#",imagename);
self.imgview.userInteractionEnabled=YES;
[imgview setImage:[UIImage imageNamed:imagename]];
[imgview setNeedsDisplay];
[imgview reloadInputViews];
str contains tag value of buttons get all values properly.but only first image displayed.
I am looking your code this code is perfect but i think the problem is due to image not present in bundle if the images is present in bundle then delete all that images and adding once again trying this might be working for you.
I Give you simple code that how to get image from ImageArray by relavent Button Click :
Suppose you have method of button is
-(void)buttonTapped:(UIButton *)sender
{
images=[[NSMutableArray alloc]init];
[images addObject:[UIImage imageNamed:#"myImage1.png"]];
.
.
.
UIImage *image = (UIImage *)[image objectAtIndex:sender.tag]; /// i Hope your each button has its own Tag.
/// here you get relavent image from button tapped by its tag;
// put this image of you imageView.
self.myImgView = [[UIImageView alloc] initWithFrame:CGRectMake("AS YOU NEED")];
self.myImgView.image = image;
[self.mainBG addSubview:self.myImgView];
}
Here make sure that your buttons have tag o to 20.
EDITE :
Use it as (just Give you example you need to update it at you code as it is)
-(IBAction)buttonTapped:(id)sender
{
NSString *images[] =
{
#"image0.png",
#"image1.png",
.
.
.
};
self.myImgView.image = [UIImage imageNamed: images[sender.tag]];
}
Are you using custom image view, if so then [imgview setNeedsDisplay] will work, else it won't work. Please do check that as well.
please try the following.
1) try self.myImgView.image = [UIImage imageNamed:#"cube.png"] try using other images like #"rectangula prizm.png", #"tourus.png" ... See what happen if you set image directly. If the image is not displaying properly, then image is not inside bundle or image name is wrong.

Where should i put my images in my iPhone project?

I'm new to iPhone dev.
Now i have a project and its directory structure looks like this :
Tutorial
\__Tutorial\
\__1.png
\__TutorialViewController.h
\__TutorialViewController.m
\__TutorialViewController.xib
\__Supporting Files\
\__Frameworks\
\__Products\
I tried to use [UIImage imageNamed:#"1.png"] to render an image to the view:
UIImage *image = [UIImage imageNamed:#"1.png"];
imageView.image = image;
[image release];
But the image doesn't shows up.
So i wonder where should i place the images ?
And additionally, if i got lots of images (like 1000 number), what should do ?
Here's my code :
#import "TutorialViewController.h"
#implementation TutorialViewController
#synthesize labelText;
#synthesize imageView;
-(void) click:(id)sender {
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:#"%#",titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:#"Blue"]){
NSLog(#"Blue");
imageView.image = [UIImage imageNamed:#"a.png"];
}else{
imageView.image = [UIImage imageNamed:#"b.png"];;
NSLog(#"Else");
}
labelText.text = newText;
[newText release];
}
You should create a folder named Resources and add images there.
UIImage *image = [UIImage imageNamed:#"IMG_0270.PNG"];
imgView_.image = image;
I had used the above code and it works fine on my system. May be you haven't connected the outlet for imgView in the interface builder.
EDIT:
The problem I found is you are not adding it as a subview. You should change your code like this :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"]];
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:#"%#", titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:#"Blue"]) {
NSLog(#"Blue");
imageView.image = [UIImage imageNamed:#"IMG_0270.png"];
} else {
imageView.image = [UIImage imageNamed:#"b.png"];
NSLog(#"Else");
}
[imageView setFrame:CGRectMake(10, 10, 230, 123)];
[self.view addSubview:imageView];
// labelText.text = newText;
[newText release];
there is no prbm with ur code. but Some time image become corrupt , due to this reason it not show, try some other images , and if image is not added at bundle path add at
project target > build phase > copy bundle Resources
. might be it will help.
You have problem at this statement
[image release];
The easiest way to remember when to use release is NARC keyword :)
Always remember to make release call in 4 cases.
N New
A Alloc
R Retain
C Copy
Try to remove the first line of your method, if you've connected the UIImageView in the xib you don't need to re-alloc it. Moreover call self.imageView.image instead just imageView.image.

iOS : Cancel a Void function

I have created a method for example changeColor for calling the method we use
[self changeColor];
but how can I cancel these method ?
Edited :
here is my code I have several buttons which they add some image to the image view
- (void) setImagesFrame1 {
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:12];
for (int i = 0; i <= 12; i++) {
NSString *fileName = [NSString stringWithFormat:#"a%d.png",i];
UIImage *j = [UIImage imageNamed:fileName];
UIImageView *tempImage = [[UIImageView alloc]initWithImage:j];
[imageArray addObject:tempImage];
[self createPageWithImage:tempImage forPage:i];
}
}
- (void) setImagesFrame2 {
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:12];
for (int i = 0; i <= 12; i++) {
NSString *fileName = [NSString stringWithFormat:#"s%d.png",i];
UIImage *j = [UIImage imageNamed:fileName];
UIImageView *tempImage = [[UIImageView alloc]initWithImage:j];
[imageArray addObject:tempImage];
[self createPageWithImage:tempImage forPage:i];
}
}
and so on ...
I call my methods with this action :
- (IBAction)openFrames:(UIButton *)sender {
[captureView addSubview:framesPreviewView];
[framesPreviewView sendSubviewToBack:captureView];
framesPreviewView.frame = CGRectMake(img.bounds.origin.x, img.bounds.origin.y, img.bounds.size.width, img.bounds.size.height);
//buttons
if (sender == frame1) { [self setImagesFrame1]; }
if (sender == frame2) { NSLog(#"frame2"); }
if (sender == frame3) { [self setImagesFrame3]; NSLog(#"frame3"); }
}
when I press frame1 button the images will be added to my view , the problem is when I press frame2 button the images of this method also add to my view I need avoid this situation , it means when I touch every button the methods of other button should be canceled
To leave a void function execute the return statement...
return;
The last view on your subview array will be the image, You should use the method [view removeFrom superView];
However, to do that you need to use fast enumeration(for-In loop) over the subviews array.
If you want to be completely sure , you can check the retuned object, if it belongs to UIImageView Class. Then you should execute remaining statements in you setImages method.
Example: `
UIView *tempView;
for(tempView in self.subviews) {
if([tempView isKindOfClass:[UIImageView class] ]){
[tempView removeFromSuperView];
}
}
`
You can:
disable frame2 button when frame1 is pressed
enable frame2 button when setImagesFrame1 ended
Something like this:
- (void) setImagesFrame1 {
frame2.enabled = NO;
[... your code ...]
frame2.enable;
}
So if someone press button 1, he can't press button 2 (and redraw something) until all operations are completed.

check which image has been set to imageview

I have used the animated images on image view concept. It is working fine.Now whenever when I touch that image view I want to check which image is placed on that image view. How should I do this. I have used the following code to make the images animated.
addimage.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Softindia.png"],
[UIImage imageNamed:#"images.png"],
nil];
addimage.animationDuration=25.0;
addimage.animationRepeatCount=0;
[addimage startAnimating];
[self.view addSubview:addimage];
Now what should I do on the touch event of image view. Please provide me some solution to resolve this.
Thanks in advance.
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagetap)] autorelease];
[imgview addGestureRecognizer:tapGesture];
-(void)imagetap
{
// your code what ever you want on touch
}
I am affraid that there is no such property inside UIImage which help you to find image name.
you can use this tricky method for solving your problem.
create an property
#property(nonatomic, retain) NSMutableArray * imageArray;
and then synthesis in .m file
#synthesis imageArray
afterward perform following action
self.imageArray = [[NSMutableArray alloc] init];
UIImage *myImage1 = [UIImage imageNamed:#"Softindia.png"];
[imageArray addObject:myImage1];
//Add Second Image
UIImage *myImage2 = [UIImage imageNamed:#"images.png"];
[imageArray addObject:myImage2];
addimage.animationImages= imageArray;
and where ever you are getting your image check do following action
UIImage image = addimage.image;
index = [imageArray indexOfObject:image];
if(index == 0){
//Do action for softindia.png
}
else {
//Do action for images.png;
}

How to View Next Image in An Array Using Index/Integer - iPhone Dev

Right,
I've got an array which looks like so:
-(void) viewDidLoad;
{
imageArray = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil] retain];
}
And I have an IBAction that is triggered when a button is clicked like so:
-(IBAction)next {
UIImage *img = [imageArray objectAtIndex:0];
[imageView setImage:img];
}
The problem is that when the button is clicked, it's only ever choosing the object at index 0 (obviously!). What I want to know is how do I define the index with an integer in the .h file, and use it in the IBAction so that whenever I click the next button it will go to the next image in the array rather than to index 0 every time?
Try this:
int current_image_index = 0;
-(IBAction)next {
if (current_image_index + 1 == [imageArray count])
current_image_index = 0;
UIImage *img = [imageArray objectAtIndex:current_image_index];
[imageView setImage:img];
current_image_index++;
}