0

So i have a UIImageView and an NSMutuableArray of four UIImages.

I just made this UIImageView animated, all the four images are animating in this imageview in a series perfectly as it should be.

Now what i want is: When user tapped on ImageView,

Which Image is just tapped by USER.

 UIImageView User Intraction is enabled
 UIGestureRecognizerDelegate is Added

-(void)viewDidAppear:(BOOL)animated
{
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];
    tapGesture.numberOfTapsRequired = 1;
    [myImageView addGestureRecognizer:tapGesture];

    [self performSelectorInBackground:@selector(showAnimatedImages) withObject:nil];
    [super viewDidAppear:animated];
}

- (void) showAnimatedImages
{
    myImageView.animationImages = imagesArray;
    myImageView.animationDuration = 12.0f;
    [myImageView startAnimating];

}

- (void)handleTaps:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateRecognized)
    {
        UIImage *selectedImage = [myImageView image]; // return nil 
        //OR
        UIImage *selectedImage = myImageView.image; // return nil
        //OR
        NSData *imgData = UIImagePNGRepresentation(myImageView.image); // return nil
    }
}

as you see myImageView.image is always giving me a nil Value.

so please tell me how can i get image from this animating imageview.

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45

4 Answers4

2

This solution will be bit different way.

Basically from the animated imageview we can't get the current image.

So do the animation in some other way

-(void)animateImages
{
    count++;

   [UIView transitionWithView:imageSlideshow
                      duration:2.0f // this is caliculated as animationduration/numberofimage
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        imageSlideshow.image = [imagesArray objectAtIndex: count % [imagesArray count]];
                    } completion:^(BOOL finished) {
                        [self animateImages];
                    }];
}

Now in your tap gesture you will get the image

Ramesh Muthe
  • 811
  • 7
  • 15
2

For tap gesture not working with your edited solution, do make sure that userinteraction for your imageview is enabled and you have correctly set up the gesture for the tapgesture. If it still doesnt work then :

I am not proud of this solution and yes it is not a very good solution but if you don't find anything else you can always use this. You can do find which image is currently selected by manually calculating that.

- (void) showAnimatedImages
{
    float duration = 6;

    index = 0; // declared in .h file

    imageView.animationImages = imagesArray;
    imageView.animationDuration = duration;
    [imageView startAnimating];

    float secs = duration/[imagesArray count];

    // timer also declared in .h file
    timer = [NSTimer scheduledTimerWithTimeInterval:secs target:self  selector:@selector(changeToNextImage) userInfo:nil repeats:YES];
}
-(void)handleTaps:(UITapGestureRecognizer*)sender
{
    NSLog(@"current Selected Image is at index %d",index);
}

-(void)changeToNextImage
{
    index++;
    if(index == [imagesArray count])
        index = 0;
}

Make sure to invalidate the timer when you are done with the animations.

Abdul91
  • 708
  • 5
  • 16
  • hmmm I created and new project and tested this code before posting it here, can you debug and check what value you get in secs variable ? I will be available again in about 45mins. – Abdul91 Nov 05 '14 at 13:35
  • not at all, but your trick. Didn't you check my self posted answer? Please check it. :) – M Zubair Shamshad Nov 06 '14 at 09:17
  • Were you able to find a better solution ? Looks like iOS API still do not let us access the current image index. This works when duration is set to a large enough value, but I have used it for drop in replacement of GIF kinda solution, and duration are all in ms and it never worked for me :( – GoodSp33d Jan 15 '18 at 11:07
0
enter code here

if (myImageView.image){
    // Image on Imageview
}else {
    // Image is not available on Imageview 
}
The above condition always go to else block , better follow this post 
http://stackoverflow.com/questions/12858028/detect-which-image-was-clicked-in-uiimageview-with-animationimages
srinivas n
  • 640
  • 4
  • 20
0

Thanks to everyone who tried to help me on this issue.

I am posting the solution i just found from the different suggestions on my Question and from some other posts too.

in my viewDidAppear method

-(void)viewDidAppear:(BOOL)animated
{
    timer = [NSTimer scheduledTimerWithTimeInterval:6.0f target:self selector:@selector(showAnimatedImages) userInfo:nil repeats:YES];
}

and the Selector Method is

- (void) showAnimatedImages
{
    if (index > [imagesArray count])
    {
        index = 0; // start from first index again
    }
    myImageView.image = [imagesArray objectAtIndex:index];
    //NSLog(@"View index is = %d",index);
    index ++;
}

and Tap Gesture Handling method,,, as i have only four images in Array.

- (void)handleTaps:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateRecognized)
    {
        if (index == 1) 
        {
            // do related stuff 
        }
        else if (index == 2)
        {
            // do related stuff
        }
        else if (index == 3)
        {
            // do related stuff
        }
        else if (index == 4)
        {
            // do related stuff
        }
    }
}

before moving to next view or scene i just do

[timer invalidate];

So UIImageView Animation and Image Selection on Tap Gesture is achieved ... :)

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
  • fair enough but you are no more using the animation provided by the imageview. Also, did u try calling showAnimatedImages in viewdidappear or where ever u want to start the animation - from my answer ? (assuming you had already initialized the images array before calling the method). showAnimatedImages from my answer was just to be called once or whenever u wanted to start animation again. And timer invalidated of course when u wanted to stop animation. :) – Abdul91 Nov 06 '14 at 10:09
  • @Abdul91 yes ofcourse, i initialized images Array before calling the animation method. And your Solution was pretty helpful to me, but it was giving me index 0 on every tap. Thanks again for your great NSTimer Trick . :) – M Zubair Shamshad Nov 06 '14 at 12:08
  • Actually using this way of timer the selector method was not even calling. so thats why index remains at 0. – M Zubair Shamshad Nov 06 '14 at 12:09
  • np, glad I was able to help. – Abdul91 Nov 06 '14 at 12:39