代码之家  ›  专栏  ›  技术社区  ›  tadejsv

NSthread上的图像视图未更新

  •  3
  • tadejsv  · 技术社区  · 14 年前

    我正在尝试制作一个测试应用程序来测量线程的性能。但是,我尝试在线程上将图像设置为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
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   tia    14 年前

    技术上, NSThread 本身不会自动创建运行循环,然后 performSelector:onThread 如果你只是用这种方式创建线程,就不会工作。

        2
  •  5
  •   jmans    14 年前

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

    performSelectorOnMainThread:...
    

    对于方法未运行的问题,请尝试以下方法:

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

    (在setImage2后添加冒号)。

    另外,我不认为这两个nsthread对象(T1和T2)是必要的,它们也可能造成内存泄漏。