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.
Related
I need to add more than one button (its depend upon array count) to UIScrollview.Now i am using the following code.This code is working properly but more time ( delay for adding button) taking for this function.Please help me..
for (int i=0; i< [imageArray count]; i++) {
NSURL *url = [NSURL URLWithString:[[imgArray objectAtIndex:i]objectForKey:#"url"]];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(xp, 0, 75, 75);
[button1 setImage:img forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button1.layer.borderColor = [UIColor whiteColor].CGColor;
button1.layer.borderWidth = 2.0;
[mScrollView addSubview:button1];
xp += button1.frame.size.width+15;
}
Because you are loading your image from server so it blocks your main thread till image load itself completely. Try loading image in different thread
Below is an example that shows how to load image on diffrent thread
Add your button object and url in an array (this can be written inside your for loop)
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:cell.businessLogoImageView];
[array addObject:#"your Url"];
[NSThread detachNewThreadSelector:#selector(loadImage:) toTarget:self withObject:array];
[array release];
array = nil;
now implement loadImage
-(void)loadImage:(NSArray *)objectArray
{
UIImageView *tempImageView = (UIImageView *)[objectArray objectAtIndex:0];
tempImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[objectArray objectAtIndex:1]]]];
}
For the ready made project you can use the following link.
As you are loading images from server it should also make CACHE of the images , so as not to download images again and again,
So, there should be something like lazyloading( this will not solve by just running in background)
LAZYLOADING scrollView
Get SDWebImage from GitHub. There's a category in it called UIButton+WebCache which can load your button's images asynchronously as well as store them on disk and memory to speed up their subsequent reloads.
#import "UIButton+WebCache"
....
UIButton *button...
[button setImageWithURL:[NSURL urlWithString:#"www.etc.com/img.jpg"] forControlState:UIControlStateNormal];
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.
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;
}
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];
}
}
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++;
}