如何从iOS中的动画UIImageView获取图像

时间:2014-11-05 11:33:39

标签: ios objective-c iphone uiimageview

所以我有一个UIImageView和一个四个UIImages的NSMutuableArray。

我刚刚对这个UIImageView进行了动画处理,所有四个图像都在这个图像视图中以完美的方式进行动画制作。

现在我想要的是:当用户点击ImageView时,

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
    }
}

如你所见myImageView.image总是给我一个零值。

所以请告诉我如何从这个动画图像视图中获取图像。

4 个答案:

答案 0 :(得分:2)

这种解决方案会有所不同。

基本上从动画图像视图中我们无法获得当前图像。

以其他方式做动画

-(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];
                    }];
}

现在,在您的点按手势中,您将获得图像

答案 1 :(得分:2)

对于不使用已编辑解决方案的点击手势,请确保已启用图像视图的用户交互,并且您已正确设置了针对该点击的手势。如果它仍然无法工作:

我并不为这个解决方案感到自豪,是的,这不是一个非常好的解决方案,但如果你找不到任何其他东西,你可以随时使用它。 您可以通过手动计算当前选择的图像来找到。

- (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;
}

确保在完成动画后使计时器无效。

答案 2 :(得分: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

答案 3 :(得分:0)

感谢所有在这个问题上帮助我的人。

我发布的解决方案我刚刚从我的问题的不同建议和其他一些帖子中找到。

在我的viewDidAppear方法

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

和选择器方法

- (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 ++;
}

和Tap Gesture Handling方法,,,因为我在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
        }
    }
}

在移动到下一个视图或场景之前我只是做

[timer invalidate];

因此,在Tap手势上实现UIImageView动画和图像选择......:)