【问题标题】:How to Get Image from Animated UIImageView in iOS如何从 iOS 中的动画 UIImageView 获取图像
【发布时间】:2015-01-01 14:03:58
【问题描述】:

所以我有一个 UIImageView 和一个包含四个 UIImage 的 NSMutuableArray。

我刚刚制作了这个 UIImageView 动画,所有四个图像都在这个 imageview 中完美地动画化了。

现在我想要的是:当用户点击 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 总是给我一个 nil 值。

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

【问题讨论】:

标签: ios objective-c iphone uiimageview


【解决方案1】:

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

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

以其他方式制作动画

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

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

【讨论】:

  • 现在只是没有检测到点击手势...... :(
  • 已将点击手势添加到 uimageview。您可以发送当前代码
  • 你使用了我建议的逻辑并且你没有投票
  • 您的建议对我来说更可靠和有帮助,但我发现了 abdul91 建议的 NSTimer Suggestion 的结果。
  • 我投票赞成您的帖子,因为您告诉我:我们无法从动画 imageView 获取图像。 :)
【解决方案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;
}

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

【讨论】:

  • hmmm 我创建了新项目并在此处发布之前测试了此代码,您可以调试并检查您在 secs 变量中获得的值吗?我将在大约 45 分钟后再次有空。
  • 根本不是,而是你的把戏。你没有检查我自己发布的答案吗?请检查一下。 :)
  • 您能找到更好的解决方案吗?看起来 iOS API 仍然不允许我们访问当前的图像索引。当持续时间设置为足够大的值时,此方法有效,但我已将其用于替换 GIF 有点解决方案,并且持续时间都以毫秒为单位,它从未对我有用:(
【解决方案3】:
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

【讨论】:

  • 我只想要图像。所以其他部分对我来说毫无意义。
  • 您发布的链接看起来很有帮助,请让我检查一下。
  • 您使用的是苹果Imageview默认属性“animatedimages”,无法获取该属性的显示图像。这就是 url 发布的方式。
【解决方案4】:

感谢所有试图帮助我解决这个问题的人。

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

在我的 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 ++;
}

和点击手势处理方法,,,因为我在数组中只有四个图像。

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

这样就实现了 UIImageView 动画和点击手势上的图像选择... :)

【讨论】:

  • 很公平,但您不再使用 imageview 提供的动画。另外,您是否尝试在 viewdidappear 中调用 showAnimatedImages 或您想在哪里开始动画 - 从我的回答中? (假设您在调用该方法之前已经初始化了图像数组)。我的回答中的 showAnimatedImages 只是被调用一次,或者当你想再次开始动画时调用。当您想停止动画时,计时器当然会失效。 :)
  • @Abdul91 是的,当然,我在调用动画方法之前初始化了图像数组。你的解决方案对我很有帮助,但每次点击它都会给我索引 0。再次感谢您出色的 NSTimer 技巧。 :)
  • 实际上使用这种计时器方式,甚至没有调用选择器方法。所以这就是为什么索引保持在 0。
  • np,很高兴我能提供帮助。
猜你喜欢
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
相关资源
最近更新 更多