0
votes

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.

4
check the post of mineRamesh Muthe
if (myImageView.image){srinivas n

4 Answers

2
votes

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

2
votes

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.

0
votes
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
0
votes

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 ... :)