View images without updating on NSThread

I am trying to create a test application that measures the performance of threads. However, I am trying to install an image in imageview in a stream, and it is not updating. In fact, the setImage1 and setImage2 methods are not even called. Any ideas? See Code for more 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
+3
source share
2 answers

Technically, NSThreadit doesn’t automatically create a startup loop, then performSelector:onThreadit won’t work if you just create a thread this way.

+3
source

UIView . , . , ( ), UIImageView

performSelectorOnMainThread:...

, :

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

( setImage2).

, , NSThread (t1 t2) , , , .

+5

Source: https://habr.com/ru/post/1773281/


All Articles