从 NSTimer 方法更新 uiimageview 时出现问题

发布于 2024-10-01 18:48:14 字数 656 浏览 4 评论 0原文

我在从 NSTimer 调用的方法中更新 UIImageView 的图像时遇到问题。 它在 viewDidLoad 方法中工作,我在其中执行类似的操作 [imageView setImage:[self getNextImage]];

稍后我做

NSTimer* readTimer = [NSTimer ScheduledTimerWithTimeInterval:1 target:self selector:@selector(readSource:) userInfo:nil Repeats:NO ];

[readTimer fire];

有效并且 readSource 方法被调用。 我在那里放置了这段代码:

UIImageView* tempImageView = (UIImageView*)[containerView viewWithTag:nextDefragSourcePosition ];

它返回的正是我想要的 imageView - 但以下行不会更改 UIImageView 的图像

[tempImageView setImage:dataBeingReadImage];

有什么想法吗?提前致谢。 平子

I have problems updating the image of an UIImageView from within a method that is called from a NSTimer.
It works within the viewDidLoad method where I do something like this
[imageView setImage:[self getNextImage]];

later on I do

NSTimer* readTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(readSource:) userInfo:nil repeats:NO];

[readTimer fire];

that works and the readSource method gets called.
There I put this code:

UIImageView* tempImageView = (UIImageView*)[containerView viewWithTag:nextDefragSourcePosition ];

That returns exactly the imageView I want to have -
but the following line does not change the image of the UIImageView

[tempImageView setImage:dataBeingReadImage];

Any ideas ? Thanks in advance.
Heiko

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-10-08 18:48:14

您是否知道您正在从与创建 UIImageView 的线程不同的线程更新 GUI?您可以在主线程上执行选择器来解决此问题。

只需看一下performSelectorOnMainThread 方法即可。只需从 NSTimer 触发时调用的方法(readSource:) 中调用它,并为其提供要显示的图像即可。然后只需从该选择器(而不是 readSource 选择器)更新 UIImageView 即可,它应该可以工作。

确实没有那么多代码可供我继续。

You are aware that you are updating the GUI from a different thread than where the UIImageView is created? You can execute selectors on the main thread to work around this problem.

Just take a look at the performSelectorOnMainThread method. Just call that from the method that gets called when your NSTimer triggers (readSource:) and supply it with the image you want to display. Then just update the UIImageView from THAT selector (not the readSource one) and it SHOULD work.

There really isn't that much code for me to go on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文