如何确保UIImageView的setImage在下一帧之前完成

发布于 2024-11-10 11:00:32 字数 1020 浏览 4 评论 0原文

我有一些代码可以连续生成动画的 UIImage 帧。目前我正在使用可变数组来保存这些图像对象,这些图像对象可以使用 UIImageView 动画方法进行动画处理。如下所示:

NSMutableArray *myFrames...;
while ( number of frames required ) {
    create UIImage frame_xx;
    [myFrames addObject:frame_xx];
}
myUImageView.animationImages = myFrames;
myUImageView.animationDuration = time_length_for_animation;
[myUImageView startAnimating];

上面的代码工作正常,但我的问题是,当我的动画变得超过几百帧时,我开始收到内存警告,最终程序崩溃。

我理想的做法是立即显示每个帧,而不是生成一堆动画帧。这样动画可能会慢一点,但我不会受限于内存。一旦显示了frame_xx,我就不再关心该帧并想要显示下一帧。

我尝试过使用以下内容,但它们不起作用:

[myUImageView setImage:frame_xx] // This only displays the last frame generated in the loop

我还尝试创建一个新线程:

        [NSThread detachNewThreadSelector: @selector(displayImage) toTarget:myUImageView withObject:frame_xx]; // again this only shows the last frame generated in the loop

所以我正在寻求帮助..如何立即在 UIImageView 中显示图像帧,有点像 [myUImageView 之后的“刷新” setImage:frame_xxx]

对此的任何帮助将不胜感激..

KAS

I have some code that continuously generates UIImage frames for animation. Currently I am using a mutable array to save these image objects which can be animated using UIImageView animation methods. Something like following:

NSMutableArray *myFrames...;
while ( number of frames required ) {
    create UIImage frame_xx;
    [myFrames addObject:frame_xx];
}
myUImageView.animationImages = myFrames;
myUImageView.animationDuration = time_length_for_animation;
[myUImageView startAnimating];

The above code works fine, however my problem is that as my animation gets longer then few hundred frames, I start getting memory warnings and eventually the program crashes.

What I would ideally like to do is to display each frame immediately rather then generating a bunch of frames for animation. Animation may be a bit slower this way but I will not be limited to the memory. As soon as the frame_xx is displayed I no longer care for that frame and want to display the next frame.

I have tried using following and they don't work:

[myUImageView setImage:frame_xx] // This only displays the last frame generated in the loop

I have also tried creating a new thread:

        [NSThread detachNewThreadSelector: @selector(displayImage) toTarget:myUImageView withObject:frame_xx]; // again this only shows the last frame generated in the loop

So I am looking for help.. how do you display an image frame immediately in UIImageView, sort of like "flush" right after [myUImageView setImage:frame_xxx]

Any help on this will be greatly appreciated..

KAS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2024-11-17 11:00:32

您尝试过 NSTimer 吗?有更准确的方法来管理计时,但如果您的帧速率相当低,这应该可以满足基本需求。在此示例中,我使用 10fps。 (我尚未编译此代码。)

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(nextFrame:) userInfo:nil repeats:YES];

...

- (void)nextFrame:(NSTimer*)timer {
    UIImage *image = ... generate image ...
    [self.imageView setImage:image];
}

当前设计中的根本问题是,如果您希望绘制图像,则必须允许运行循环完成。您不能也不应该导致绘图发生在事件循环的中间。

Have you tried an NSTimer? There are more accurate ways to manage timing, but this should work for basic needs if your frame rate is fairly low. In this example, I'm using 10fps. (I haven't compiled this code.)

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(nextFrame:) userInfo:nil repeats:YES];

...

- (void)nextFrame:(NSTimer*)timer {
    UIImage *image = ... generate image ...
    [self.imageView setImage:image];
}

The underlying problem in your current design is that you must allow the runloop to complete if you want the image to be drawn. You cannot and should not cause drawing to happen in the middle of an event loop.

梦巷 2024-11-17 11:00:32

您是否尝试过不使用 UIImageView 并在 drawRect: 方法中绘制图像?

比如:

    [image drawInRect:rect]; // where 'rect' is the frame for the image

另外,永远不要在单独的线程中进行任何 UI 绘制。这样做你会遭受随机和持续的崩溃。 NSThread#detachNewThreadSelector 不是在 iOS 上执行多线程任务的首选方法。看看 NSOperation 和 NSOperationQueue。然后当你需要向UI用户绘制NSObject的实例方法performSelectorOnMainThread:时。

Have you tried not using UIImageView and drawing your images in a drawRect: method?

Something like:

    [image drawInRect:rect]; // where 'rect' is the frame for the image

Also, never do any UI drawing in a separate thread. You'll suffer random and persistent crashes from doing so. NSThread#detachNewThreadSelector is not a preferred way to perform multithreaded tasks on iOS. Look at NSOperation and NSOperationQueue. Then when you need to draw to the UI user the NSObject's instance method performSelectorOnMainThread:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文