2010-11-05 25 views
3

我正在嘗試製作測量線程性能的測試應用程序。但是,我試圖設置一個圖像到一個線程imageview,它不更新。實際上,setImage1和setImage2方法甚至沒有被調用。有任何想法嗎? 看看在細節NSThread上未更新圖像視圖

#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 
+0

你確定沒有調用setImage1和setImage2嗎?如果你添加一個NSLog它打印過嗎? – 2010-11-05 17:49:42

+0

100%肯定。設置斷點。 – tadejsv 2010-11-05 17:58:50

回答

3

從技術上講,NSThread本身不會自動創建運行循環,那麼如果您只是以這種方式創建線程,則performSelector:onThread將不起作用。

+0

好吧,那麼如何在線程上創建runloop – tadejsv 2010-11-05 21:36:34

+0

我剛發現這實際上是問題所在。我想我不得不花時間閱讀線程編程指南;) – tadejsv 2010-11-05 21:37:43

+0

我不確定你的項目是關於什麼的,但是在許多情況下,使用'performSelectorInBackground'就足夠了。 – tia 2010-11-05 21:46:15

5

的代碼千萬不要在當前顯示的UIView從後臺線程改變任何東西。有一百萬個不同的同步問題,你可以遇到。下載圖像的後臺線程,然後調用主線程上的另一種方法(從後臺線程),以實際更新的UIImageView

performSelectorOnMainThread:... 

至於該方法沒有運行問題,試試這個:

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

(在setImage2之後添加冒號)。

此外,我不認爲這兩個NSThread對象(t1和t2)是必要的,他們也可能造成內存泄漏。

+0

好吧,但問題是,方法setImage1和setImage2甚至沒有被稱爲 – tadejsv 2010-11-05 18:01:36

+0

好的,我添加了一個可能的解決方案,我的帖子。 – jmans 2010-11-05 18:19:07