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 ren开发者_运维问答der 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?
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.
[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();
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?
精彩评论