图像视图未在 NSThread 上更新

发布于 10-01 00:42 字数 2327 浏览 5 评论 0原文

我正在尝试制作一个测试应用程序来测量线程的性能。但是,我试图在线程上将图像设置为 imageview,但它没有更新。事实上,方法 setImage1 和 setImage2 甚至没有被调用。有什么想法吗? 详细看代码

#import "UntitledAppDelegate.h"
#import <QuartzCore/QuartzCore.h>

@implementation UntitledAppDelegate

@synthesize window, imageView1, imageView2, label1, label2, label0;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{            
    [window makeKeyAndVisible];
    return YES;
}

-(IBAction) startSeparate:(id) sender
{
 imageView1.image = imageView2.image = nil;
 double prev;

    start = prev = CACurrentMediaTime(); 
 [self setImage1];
 label1.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];

 prev = CACurrentMediaTime();  
 [self setImage2];
 label2.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];
}

-(IBAction) startThreaded:(id) sender
{
 imageView1.image = imageView2.image = nil;
 double prev;

 NSThread *t1 = [[NSThread alloc] init];
 [t1 start];
 NSThread *t2 = [[NSThread alloc] init];
 [t2 start];

    start = prev = CACurrentMediaTime(); 
    //This doesn't work
    //[self performSelector:@selector(setImage1) onThread:t1 withObject:nil waitUntilDone:NO];

    //But this does
 [self performSelectorInBackground:@selector(setImage1) withObject:nil];

 label1.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];

 prev = CACurrentMediaTime();  
 //This doesn't work
    //[self performSelector:@selector(setImage2) onThread:t2 withObject:nil waitUntilDone:NO];

    //But this does
 [self performSelectorInBackground:@selector(setImage2) withObject:nil];

 label2.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];
}

-(void) setImage1
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 UIImage *image1 = [UIImage imageNamed:@"bird.png"];
 imageView1.image = image1;
 [self pictureDone];
 [pool drain];
}

-(void) setImage2
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 UIImage *image2 = [UIImage imageNamed:@"bird.png"];
 imageView2.image = image2;
 [self pictureDone];
 [pool drain];
}

-(void) pictureDone
{
 done++;
 NSLog(@"%i", done);
 if (done == 2) {
  label0.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - start];
  done = 0;
 }
}
@end

I'm trying to make a test app which measures the performance for threads. However, I'm trying to set an image to imageview on a thread and it's not updating. In fact, the methods setImage1 and setImage2 aren't even getting called. Any ideas?
Look at the code for details

#import "UntitledAppDelegate.h"
#import <QuartzCore/QuartzCore.h>

@implementation UntitledAppDelegate

@synthesize window, imageView1, imageView2, label1, label2, label0;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{            
    [window makeKeyAndVisible];
    return YES;
}

-(IBAction) startSeparate:(id) sender
{
 imageView1.image = imageView2.image = nil;
 double prev;

    start = prev = CACurrentMediaTime(); 
 [self setImage1];
 label1.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];

 prev = CACurrentMediaTime();  
 [self setImage2];
 label2.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];
}

-(IBAction) startThreaded:(id) sender
{
 imageView1.image = imageView2.image = nil;
 double prev;

 NSThread *t1 = [[NSThread alloc] init];
 [t1 start];
 NSThread *t2 = [[NSThread alloc] init];
 [t2 start];

    start = prev = CACurrentMediaTime(); 
    //This doesn't work
    //[self performSelector:@selector(setImage1) onThread:t1 withObject:nil waitUntilDone:NO];

    //But this does
 [self performSelectorInBackground:@selector(setImage1) withObject:nil];

 label1.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];

 prev = CACurrentMediaTime();  
 //This doesn't work
    //[self performSelector:@selector(setImage2) onThread:t2 withObject:nil waitUntilDone:NO];

    //But this does
 [self performSelectorInBackground:@selector(setImage2) withObject:nil];

 label2.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - prev];
}

-(void) setImage1
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 UIImage *image1 = [UIImage imageNamed:@"bird.png"];
 imageView1.image = image1;
 [self pictureDone];
 [pool drain];
}

-(void) setImage2
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 UIImage *image2 = [UIImage imageNamed:@"bird.png"];
 imageView2.image = image2;
 [self pictureDone];
 [pool drain];
}

-(void) pictureDone
{
 done++;
 NSLog(@"%i", done);
 if (done == 2) {
  label0.text = [NSString stringWithFormat:@"%.10g", CACurrentMediaTime() - start];
  done = 0;
 }
}
@end

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

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

发布评论

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

评论(2

故人如初2024-10-08 00:42:49

切勿从后台线程更改当前显示的 UIView 中的任何内容。您可能会遇到一百万种不同的同步问题。在后台线程中下载图像,然后在主线程上(从后台线程)调用另一个方法来实际更新 UIImageView

performSelectorOnMainThread:...

至于该方法未运行的问题,请尝试这样做:(

[self performSelectorInBackground:@selector(setImage2:) withObject:nil];

在 setImage2 后面添加冒号)。

另外,我认为这两个 NSThread 对象(t1 和 t2)不是必需的,而且它们也可能会造成内存泄漏。

Never change anything in a currently displayed UIView from a background thread. There are a million different synchronization problems that you can encounter. Download the image in the background thread, and then call another method on the main thread (from the background thread) to actually update the UIImageView

performSelectorOnMainThread:...

As for the problem with the method not running, try this instead:

[self performSelectorInBackground:@selector(setImage2:) withObject:nil];

(adding a colon after setImage2).

Also, I don't think those two NSThread objects (t1 and t2) are necessary, and they are also probably creating a memory leak.

我是男神闪亮亮2024-10-08 00:42:49

从技术上讲,NSThread本身不会自动创建运行循环,那么如果您只是以这种方式创建线程,则performSelector:onThread将不起作用。

Technically, NSThread itself do not create run loop automatically, then the performSelector:onThread won't work if you just create thread that way.

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