Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用线程在图像视图中替换图像_Ios_Objective C - Fatal编程技术网

Ios 使用线程在图像视图中替换图像

Ios 使用线程在图像视图中替换图像,ios,objective-c,Ios,Objective C,我有以下代码在图像视图中的两个图像之间切换。我没有使用GCD,因为该代码是已经存在的系统的一部分,该系统已经以这种方式编码 - (void)updateimage { dispatch_async(dispatch_get_main_queue(), ^{ if (self.fileno % 2 == 0) { self.imageViewTest.image = [UIImage imageNamed:@"Image7.png"]; }

我有以下代码在图像视图中的两个图像之间切换。我没有使用GCD,因为该代码是已经存在的系统的一部分,该系统已经以这种方式编码

- (void)updateimage { 
dispatch_async(dispatch_get_main_queue(), ^{
        if (self.fileno % 2 == 0) {
            self.imageViewTest.image = [UIImage imageNamed:@"Image7.png"];
        }
        else {
           self.imageViewTest.image = [UIImage imageNamed:@"Image8.png"];
        }
       [self.imageViewTest setNeedsDisplay];
        self.filenolabel.text = [NSString stringWithFormat:@"%d", self.fileno];
    });

}
- (void)calculateFileNo {
while (1) {
    self.fileno ++;
    sleep(1);

    [self updateimage];

 }
}
- (void)viewDidLoad {
     [super viewDidLoad];
     self.detectionThread = [[NSThread alloc]initWithTarget:self  selector:@selector(calculateFileNo) object:nil] ;
    [self.detectionThread setName:@"NewThread"];
    [self.detectionThread start];
 }

filenolabel标签显示每个循环的正确编号。但有时图像无法正确切换。相同的图像在循环的2或3次迭代中显示。我希望在每次迭代中切换图像。请帮忙。提前感谢

增量位置是个问题'self.fileno++;'->把它移到调度程序

- (void)updateimage { 
dispatch_async(dispatch_get_main_queue(), ^{

        if (self.fileno % 2 == 0) {
            self.imageViewTest.image = [UIImage imageNamed:@"Image7.png"];
        }
        else {
           self.imageViewTest.image = [UIImage imageNamed:@"Image8.png"];
        }
       [self.imageViewTest setNeedsDisplay];
        self.filenolabel.text = [NSString stringWithFormat:@"%d", self.fileno];
    self.fileno ++;
    });

}
- (void)calculateFileNo {
while (1) {

    sleep(1);

    [self updateimage];

 }
}

在主线程内增加文件编号

     self.fileno++;
只有在UImageView的子类中重写drawRect时,才应该调用setNeedsDisplay,UImageView基本上是一个在屏幕上绘制某些东西的自定义视图,如线条、图像或矩形形状

因此,当您对该图形所依赖的几个变量进行更改时,应该调用setNeedsDisplay,并且为了让视图表示该更改,您需要调用该方法,该方法将在内部调用drawRect并重新绘制组件

当您更改图像的imageView或更改任何子视图时,需要调用此方法

    - (void)updateimage {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.fileno % 2 == 0) {
                self.imageView.image = [UIImage imageNamed:@"Image7.png"];
            }
            else {
                self.imageView.image = [UIImage imageNamed:@"Image8.png"];
            }
            [self.imageView setNeedsDisplay];
            self.fileno ++; // Increament your file no inside the main thread.
            self.textView.text = [NSString stringWithFormat:@"%d", self.fileno];
        });
    }

您是否应该调用
setNeedsDisplay