Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 UIImageView没有';t在for循环中更新图像_Ios_Objective C_Uiimageview - Fatal编程技术网

Ios UIImageView没有';t在for循环中更新图像

Ios UIImageView没有';t在for循环中更新图像,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我有一个CGRectarr数组,我想在for循环中将这些矩形绘制到图像上。代码如下: __block UIImage *procImage=image; for(int i=0;i<arr.count;i++){ CGRect rect=[[arr objectAtIndex:i] CGRectValue]; procImage=[self drawText:[NSString stringWithFormat:@"%d",i] inImage:p

我有一个CGRect
arr
数组,我想在for循环中将这些矩形绘制到图像上。代码如下:

 __block UIImage *procImage=image;


for(int i=0;i<arr.count;i++){
    CGRect rect=[[arr objectAtIndex:i] CGRectValue];
    procImage=[self drawText:[NSString stringWithFormat:@"%d",i] inImage:procImage atPoint:CGPointMake(rect.origin.x,rect.origin.y-rect.size.height) withRect:rect];

    [self.imageView setImage:procImage];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        procImage=[self drawText:[NSString stringWithFormat:@"%d",i] inImage:procImage atPoint:CGPointMake(rect.origin.x,rect.origin.y-rect.size.height) withRect:rect];

        [self.imageView setImage:procImage];


    }];
}
问题是imageView仅在for循环完成时更新其图像。我想创建一个很好的动画,每次在for循环中绘制一个新矩形时都会更新图像

编辑
我简化了代码。在我的例子中,我正在为每个文本块执行OCR过程,因此这将花费很多时间(可能几分钟)。在这段时间里,我想向用户展示这个过程中的一些东西,而不是每次完成一个文本块时都显示一个死板的界面。

不清楚您想要的是什么效果。是否希望在绘制每个图像和目标矩形中的文本之间有一个短暂的延迟,以便它们堆叠起来

你有两个问题。首先,在代码返回之前,实际上不会向屏幕呈现任何内容。因此,当您运行这样的循环时,图形调用只是堆叠起来,从代码返回,然后所有内容都呈现到屏幕上

即使你解决了这个问题,你的第二个问题是,在屏幕上画一幅又一幅的图像会非常快,以至于用户的眼睛看不到各个步骤

如果我对你想做的是对的,你可以用这种方式来代替

添加实例变量以跟踪当前图像的索引。将其设置为零

编写一个方法,在当前矩形处绘制当前图像和文本,然后递增索引实例变量。如果图像/矩形数组中有更多条目,请在短时间延迟后使用GCD call dispatch_在主线程上调用方法本身。这将把控制权返回到主线程,让新图形呈现到屏幕上,并让用户在绘制下一个图形之前有时间查看该图形

编辑:
正如@Paulw11在他的评论中所说的,您还可以使用重复计时器重新调用您的方法,并在数组中的条目用完后使其无效。

创建图像数组并缓存它。然后使用
UIImageView
animationImages
属性存储已创建的图像数组,并让
UIImageView
处理动画。每次构造一个映像都需要大量的过程

苹果文档:


使用重复的
NSTimer
代替for循环。这将使运行循环有机会呈现您的更改。另外,你的for循环将完成得如此之快,以至于无论你如何击败我,这些变化都是看不见的。当你输入你的评论时,我输入了一个太冗长的答案。对不起,不清楚。我编辑了这个问题以提供更多信息。这里的重点不仅仅是动画。我想让用户在我复杂繁重的过程中看到一些变化,而不是静止的图像。谢谢!我编辑了我的问题,以便更清楚地了解情况。还有,我不知道你说的“当我的代码返回时”是什么意思?是关于NSO的行动吗?我曾尝试在不将这些代码放入NSOperation块的情况下更改图像,但实际上什么也没发生。
-(UIImage*) drawText:(NSString*) text
         inImage:(UIImage*)  image
         atPoint:(CGPoint)   point
        withRect:(CGRect)rectangle
{

UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);

[text drawInRect:CGRectIntegral(rect) withAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor],
                                                       NSFontAttributeName:[UIFont boldSystemFontOfSize:50]}
 ];
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextStrokeRect(context, rectangle);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}