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

标签 ios objective-c iphone uiimageview

所以我有一个 UIImageView 和一个由四个 UIImage 组成的 NSMutuableArray。

我刚刚制作了这个 UIImageView 动画,所有四个图像都在这个 imageview 中以一系列完美的动画呈现。

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

用户刚刚点击了哪个图像

 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 总是给我一个 nil 值。

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

最佳答案

这个解决方案会有点不同。

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

以其他方式制作动画

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

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

关于ios - 如何从 iOS 中的动画 UIImageView 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756202/

相关文章:

ios - UIScrollView 用于无限幻灯片放映

ios - UIView 和 UIViewController

iphone - 在 iOS 6 中状态栏色调颜色更改为黑色

objective-c - 可以存储在可执行二进制文件中的文本数据是否有限制?

ios - 如何设置即将加载到 UITabBarController 中的 UIViewController 的属性?

iphone - 使用 Google App 引擎创建用户控制的数据服务

ios - iOS是否延迟peripheralManager :didReceiveWriteRequests: and peripheralManager:didReceiveReadRequest:?

ios - 更改方向时如何使 UITabbar selectionIndicatorImage 发生变化 - Swift

ios - Swift 中 UIScrollView 内的 UIPanGesture

ios - 如何使用SDWebImage