简体   繁体   English

在for循环中,UIImageView不会更新图像

[英]UIImageView doesn't update image while in for loop

I have an Array of CGRect arr , and I want to draw those rectangles onto an image in a for loop. 我有一个CGRect arr数组,我想在for循环中将这些矩形绘制到图像上。 This is the code: 这是代码:

 __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];


    }];
}

This is the drawText: function 这是drawText:函数

-(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;
}

The problem is the imageView only update its image when the for loop finished. 问题是imageView仅在for循环完成时才更新其图像。 I want to create a nice animation that updates the image for everytime a new rectangle is drawn during the for loop. 我想创建一个不错的动画,以便在for循环中每次绘制新矩形时都会更新图像。

Edit 编辑

I had simplified the code. 我简化了代码。 In my case, I was performing an OCR process for each text block so it would take a lot of time (maybe several minutes). 以我为例,我正在为每个文本块执行OCR处理,因此将花费很多时间(可能是几分钟)。 During that time I want to show the users something of the process instead of a dead interface for each time a text block is done. 在这段时间里,我想向用户展示每个过程的过程,而不是每次文本块完成时都显示死界面。

Create your images array and cache it. 创建图像数组并对其进行缓存。 Then use UIImageView animationImages property to store the array of images you have created and let UIImageView handle the animation. 然后使用UIImageView animationImages属性存储已创建的图像数组,并让UIImageView处理动画。 Constructing an image every single time is process intensive. 每次构造图像都需要大量的过程。

Apple Documentation: 苹果文档:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImageView_Class/index.html#//apple_ref/occ/instp/UIImageView/animationImages https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImageView_Class/index.html#//apple_ref/occ/instp/UIImageView/animationImages

It isn't clear what effect you are after. 尚不清楚您要做什么。 Do you want a short delay between drawing each image and text in it's target rectangle so they stack up? 您是否希望在目标矩形中绘制每个图像和文本之间有短暂的延迟,以便它们堆叠在一起?

You have 2 problems. 您有2个问题。 First, nothing is actually rendered to the screen until your code returns. 首先,在代码返回之前,实际上什么都不会呈现到屏幕上。 Thus when you run a loop like that, the drawing calls just stack up, you return from your code, and then everything gets rendered to the screen. 因此,当您运行这样的循环时,图形调用便会堆积起来,您从代码中返回,然后所有内容都呈现到屏幕上。

Even if you fix that, your second problem is that drawing image after image after image to the screen will be so fast that the user's eyes won't see the individual steps. 即使您修复了该问题,您的第二个问题是将一个接一个的图像绘制到屏幕上的速度如此之快,以至于用户的眼睛看不到各个步骤。

If I'm right about what you're trying to do, you could do it this way instead. 如果我对您要执行的操作是正确的,则可以改用这种方式。

Add an instance variable to keep track of the index of the current image. 添加一个实例变量以跟踪当前图像的索引。 Set it to zero. 将其设置为零。

Write a method that draws the current image and text at the current rectangle, then increments the index instance variable. 编写一种方法,该方法在当前矩形处绘制当前图像和文本,然后递增index实例变量。 If there are more entries in your array of images/rectangles, have the method call itself on the main thread after a short delay using the GCD call dispatch_after. 如果图像/矩形数组中有更多条目,请在短暂延迟后使用GCD调用dispatch_after在主线程上调用该方法。 That would return control to the main thread and let the new drawing render to the screen, and give the user time to see that drawing before the next one is drawn. 这将控制权返回到主线程,并让新图形呈现到屏幕上,并让用户有时间在绘制下一个图形之前看到该图形。

EDIT: 编辑:

As @Paulw11 says in his comment you could also re-invoke your method using a repeating timer and simply invalidate it once you run out of entries in your array. 就像@ Paulw11在他的评论中所说,您还可以使用重复计时器重新调用您的方法,并在数组中的所有条目用完后直接使它无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM