I am using a UIImageView and about 5 images that is changing time to time with the help of timer.
Can we get currently set image name in UIImage.I google it and found a that by inserting image names to array and by setting tag to imageView we can get image name by its tag but in my case there is only one imageView and images are 5.My code is given below:--
[_imgView setImage:[UIImage imageNamed:#"tops.png"]];
imgArray=[[NSArray alloc] initWithObjects:#"tops.png",#"position_conv.png",#"stocks.png",#"home.png",#"report.png",nil];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(change) userInfo:nil repeats:YES];
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
}
Thanks!
you can use Accessibility identifier property of UIImageView's image.Set image accessibility identifier in your method and get anywhere you want.
-(void)change
{
int randNum = rand() % (4 - 0) + 0;
[_imgView setImage:[UIImage imageNamed:[imgArray objectAtIndex:randNum]]];
[_imgView.image setAccessibilityIdentifier:[imgArray objectAtIndex:randNum]];
}
- (IBAction)currentImgName:(id)sender {
NSLog(#"image name is %#", [_imgView.image accessibilityIdentifier]);
}
You can use setAccessibilityIdentifier method for any subclass of UIView
UIImageView *image ;
[image setAccessibilityIdentifier:#"file name"] ;
NSString *file_name = [image accessibilityIdentifier] ;
Related
I am wanting to press a button and alternate between two images with one UIImageView.
Right now when I run it, I press the button, the image changes but will not change back. What do I need to change in this action code to make it alternate?
- (IBAction)Change:(id)sender {
image.image = [UIImage imageNamed:#"car.png"];
}
Try following code :
- (IBAction)Change:(id)sender {
if ([sender isSelected])
{
imageView.image = [UIImage imageNamed:#"car.png"];
[sender setSelected:NO];
}
else
{
imageView.image = [UIImage imageNamed:#"bike.png"];
[sender setSelected:YES];
}
}
To Display sequence image on button click.
.h file :
int counter;
.m file :
in viewDidLoad initialize counter = 0
Then
- (IBAction)Change:(id)sender {
counter++;
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",counter]];
}
And give your image name like 1.png, 2.png, 3.png and so on...
You have to maintain a flag.
In Somewhere before tapping the button, assign the flag
BOOL isFirstImageShown=YES;
Then on your button action
- (IBAction)Change:(id)sender {
if(isFirstImageShown)
{
isFirstImageShown=NO;
yourImageView.image = [UIImage imageNamed:#"yourSecondImage.png"];
}
else
{
isFirstImageShown=YES;
yourImageView.image = [UIImage imageNamed:#"yourFirstImage.png"];
}
}
You want that on button click you need another image you have to first set the image when you load the view and then on button click.
I want to change the image in image view on button click but it does not changing. I am using JPG image to load in image View
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
NSInteger selectedIndex = [tappedButton tag];
if (selectedIndex==0)
{
UIImage *modelimage = [UIImage imageNamed:#"compositone.jpg"];
modelImagewView.image=modelimage;
}
else
{
UIImage *modelimage = [UIImage imageNamed:#"composittwo.jpg"];
modelImagewView.image=modelimage;
}
}
Try this format - modelImagewView.image = [UIImage imageNamed:#"composittwo.jpg"];
Also, make sure you have set tag numbers properly so that selectedIndex is not always 0. At the same time check if you have linked this IBAction to the appropriate button.
Try This Code
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
if (tappedButton.tag==0)
{
modelImageView.image=[UIImage imageNamed:#"compositone.jpg"];
}
else
{
modelImageView.image=[UIImage imageNamed:#"composittwo.jpg"];
}
}
hey i am running into a problem.
i maade an image object that gets added every 2 seconds by an nstimer.
and an update timer updates it so the image goes forward.
but it only goes forward until a new one gets added and i can't solve why.
this is the method for adding it.
-(void)addTarget {
UIImage *image1=[UIImage imageNamed:#"enemy.png"];
image=[[UIImageView alloc]initWithImage:image1];
image.frame=CGRectMake(0,0,50,50);
[self.view addSubview:image];
image.center = CGPointMake(150, 150);
image.tag = 1;
[_targets addObject:image];
[image release];
}
self explaining.
-(void) update {
image.center = CGPointMake(image.center.x+2, image.center.y);
}
and this spawns them.
-(void) spawn {
[self addTarget];
}
Its because you are constantly reallocating the image. You need to be creating a new image1 variable every time and then adding it to an NSMutableArray.
Then in the update method use a for loop to move each image in the array's centre to whatever point.
- (void)update {
for (UIImage *image in _targets) {
image.center = CGPointMake(image.center.x+2, image.center.y);
}
}
I am sure I am doing something stupid here. I build a category on top of UIButton which I want it to take all of the background images assigned to it (different states) and convert them to stretchable versions and reapply them back to the button.
- (void)enableBackgroundImageStrechingWithLeftCapWidth:(float)leftCapWidth withTopCapHeight:(float)topCapHeight;
{
UIImage *backgroundimageNormal = [self backgroundImageForState:UIControlStateNormal];
if (backgroundimageNormal != nil)
{
UIImage *stretchImage = [backgroundimageNormal stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateNormal];
}
UIImage *backgroundimageSelected = [self backgroundImageForState:UIControlStateSelected];
if (backgroundimageSelected != nil)
{
UIImage *stretchImage = [backgroundimageSelected stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateSelected];
}
UIImage *backgroundimageHighlighted = [self backgroundImageForState:UIControlStateHighlighted];
if (backgroundimageHighlighted != nil)
{
UIImage *stretchImage = [backgroundimageHighlighted stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateHighlighted];
}
UIImage *backgroundimageDisabled = [self backgroundImageForState:UIControlStateDisabled];
if (backgroundimageDisabled != nil)
{
UIImage *stretchImage = [backgroundimageDisabled stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateDisabled];
}
}
Seems to work except the button is now not clickable
It seems that the highlighted state causes the issue!
I have removed the highlighted block and it works fine?
If you do not have custom images for highlighted/disabled states, but rely on UIButton to apply the highlight/disabled effect, then [self backgroundImageForState: UIControlStateHighlighted] will NOT return nil. It will instead return a pointer to the normal state image.
Using your code, you are then effectively setting identical images for all states. They look like custom images to the framework, though. This disables the built-in highlight/disabled effects.
I'm using animation method of for implementing slide show in UIimageView as:
mainSlideShowImageView.animationImages=images;
mainSlideShowImageView.animationDuration = 75.00;
mainSlideShowImageView.animationRepeatCount = 0; //infinite
[mainSlideShowImageView startAnimating];
Now, what I want is to know which image is currently on image view screen.
How can I do so?
If it is not possible please tell that too.
Edit: 'images' here is array of UIImage.
AFAIK, there is no method which returns imageview's current image , however I have created a workaround for this....
array= [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],[UIImage imageNamed:#"2.jpg"],[UIImage imageNamed:#"3.jpg"],[UIImage imageNamed:#"4.jpg"],[UIImage imageNamed:#"5.jpg"],nil];
mainSlideShowImageView.animationImages= array;
mainSlideShowImageView.animationDuration=15.0;
i=0;
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(increase) userInfo:nil repeats:YES];
[mainSlideShowImageView startAnimating];
-(void)increase
{
i++;
if (i>4) {
i=0;
}
}
-(void)getimage
{
UIImage* im = [array objectAtIndex:i];
// im is the current image of imageview
}
I have taken 5 images here and animation duration of 15 , that means image will change at every 3 seconds, so I have kept timer to fire at every 3 seconds ... and since array contains 5 images ... so if 'i' goes beyond 4 I have put it back to 0 in method increase