UIImageView 是否有一个属性来指示 setImage 已完成并且动画已准备好?

发布于 2024-11-06 15:37:14 字数 532 浏览 4 评论 0原文

刚刚开始使用 Obj-C 和 iOS 编程。我有一些代码将 imageView 加载到(隐藏:是)UIView 中 -

[bgImageView setImage:[UIImage imageNamed:@"Filename.jpg"];

然后代码在 fgImageView 和 bgImageView 上设置并提交动画。 由于某些图像可能很大,因此有时新 bgImage 中的动画不会使用其中的新背景图像进行渲染,而是“停止并显示”。请注意,在 setImage 期间不会发生停顿。它发生在代码中的第二个animationCommit期间。因为animationCommit强制应用程序等待,直到这个ImageView的动画完成。

我正在寻找的是 UIImageView 上的“setImage commit”,

我宁愿显示一个微调器,直到加载图像,然后继续播放动画,但是 UIImageView 似乎没有 isLoaded 属性班级。

有没有一种简单的方法可以确定 UIImageView 是否已通过 setImage 调用完成?

Just getting started with Obj-C and iOS programming. I have some code that loads and imageView into a (HIDDEN: YES) UIView - simply

[bgImageView setImage:[UIImage imageNamed:@"Filename.jpg"];

The code then sets and commits animations on the fgImageView and the bgImageView.
Because some of the images can be large, occasionally the animation from the new bgImage does not render with the new background image in it and instead 'stalls and displays'. Note that the stall does not happen during setImage. It happens during the second animationCommit later in code. Because animationCommit forces the application to wait until the animation of this ImageView is done.

What I'm looking for is sort of a 'setImage commit' on UIImageView,

I'd much rather display a spinner until the image is loaded, then proceed with the animation, however there doesn't appear to be an isLoaded property for UIImageView class.

Is there a simple way to determine that the UIImageView is done with the setImage call?

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

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

发布评论

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

评论(3

梦断已成空 2024-11-13 15:37:14

我的直觉是 setImage 是同步处理的,特别是因为您正在谈论应用程序如何停止。您需要在 NSOperation 中运行 setImage 以允许在后台发生这种情况。

http://developer.apple.com/ library/ios/#documentation/cocoa/reference/NSOperation_class/Reference/Reference.html

NSInvocationOperation *myOp = [[NSInvocationOperation alloc] initWithTarget:myImageView selector@selector(setImage:) object:aUIImage];
[myOp addObserver:(NSObject *)self forKeyPath:(NSString *) @"isFinished" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL];
[anOpQueue addOperation:myOp];

然后,您将处理 KVO 操作并在那里删除微调器。

My gut is that setImage is handled synchronously, especially since you're talking about how the app stalls. You'd want to run setImage in an NSOperation to allow that happen in the background.

http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSOperation_class/Reference/Reference.html

NSInvocationOperation *myOp = [[NSInvocationOperation alloc] initWithTarget:myImageView selector@selector(setImage:) object:aUIImage];
[myOp addObserver:(NSObject *)self forKeyPath:(NSString *) @"isFinished" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL];
[anOpQueue addOperation:myOp];

You'd then handle the KVO operation and handle removing the spinner there.

∞梦里开花 2024-11-13 15:37:14

[UIImageView setImage:] 不是异步的:它是在消息返回之前立即完成的。如果视图没有立即显示图像,可能是因为一些惰性 UIImage 代码。我的猜测是 [UIImage imageNamed:] 只是引用包中的文件并仅在请求图像数据后加载它。

您可以尝试通过向图像发送 CGImage 消息来强制加载图像:

[bgImageView.image CGImage];

如果这仍然没有触发图像加载,您必须将其绘制在某处:

UIGraphicsBeginImageContext((CGSize){1, 1});
[bgImageView drawInRect:(CGRect){{0, 0}, {1, 1}}];
UIGraphicsEndImageContext();

[UIImageView setImage:] is not asynchronous: It's done immediately before the message returns. If the view does not show the image instantly it's probably because of some lazy UIImage code. My guess is that [UIImage imageNamed:] just references the file in the bundle and loads it only after the image data is requested.

You could try to force the image to load by sending it a CGImage message:

[bgImageView.image CGImage];

If this still does not trigger the image to be loaded you have to draw it somewhere:

UIGraphicsBeginImageContext((CGSize){1, 1});
[bgImageView drawInRect:(CGRect){{0, 0}, {1, 1}}];
UIGraphicsEndImageContext();
巷雨优美回忆 2024-11-13 15:37:14

UIImageView - (BOOL)isAnimating 中有一个方法,因此您可能会使用:

if (![bgImageView isAnimating]) {
    // continue to next image;
}

这样就足够了,还是正在进行中的其他动画会给出错误响应?

There is a method in UIImageView - (BOOL)isAnimating, so you could potentially use:

if (![bgImageView isAnimating]) {
    // continue to next image;
}

Would that suffice or is there other animation in progress that would give false responses?

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