从 NSTimer 方法更新 uiimageview 时出现问题
我在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否知道您正在从与创建 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.