How to optimize image loading during core animation?
How to optimize image loading during core animation?
- Subject: How to optimize image loading during core animation?
- From: Gabriel Zachmann <email@hidden>
- Date: Wed, 16 Feb 2011 12:14:03 +0100
I am implementing a screensaver that is basically a photo show.
The photos are animated using Core Animation (nothing complicated).
The problem now is that when the screensaver switches to the next photo, the animation judders.
The judder happens, as far as I can tell, only with large images, e.g., 7 MB JPEGs.
(You can find an excerpt of my code at the end of this post.)
I suspect this is because I load the image at the beginning of a photo switch, which can take a lot of CPU power, which could cause a decrease in frame rate for my screensaver, which in turn would cause the judder, because Core Animation probably tries to make the animation "travel" the same "distance" for a given time span, but with less frames in-between.
So, the first question is: is my suspicion correct, or could it be any other reason?
The next question, the, is: what can I do against it?
One idea I was toying with is to load the images half-way between an image switch.
But would that really help? Wouldn't that just cause the judder to occur at that time (note that the photos are always animated!)
So, what else could I do?
Can I somehow load images half-way between image switches *and* tell the OS to use only very little CPU power for it?
I am sort of hesitant to fork another thread just for the image loading, because the screensaver already runs in several threads parallel, when the user has several monitors attached to their Mac ...
(Or would that be painless?)
Any and all ideas, hints, pointers, examples, and suggestions will be highly appreciated!
Best regards,
Gabriel.
PS:
Here is an excerpt of my code
- (void) animateOneFrame
{
if ( time is not up yet )
return;
// load new image from disk
NSURL * url = [NSURL fileURLWithPath: [self absolutePathFor: filename_] isDirectory: NO];
...
CGImageSourceRef sourceRef = CGImageSourceCreateWithURL( (CFURLRef) url, NULL );
CFDictionaryRef fileProps = CGImageSourceCopyPropertiesAtIndex( sourceRef, 0, NULL );
... [check whether image is eligible for display; if not, return]
CGImageRef imageRef = CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL );
CFRelease(sourceRef);
...
// do garbage collection a bit later, to prevent judder
[NSTimer scheduledTimerWithTimeInterval: 2.3 target: self selector: @selector(doGarbageCollection:) userInfo: nil repeats: NO];
CALayer * newlayer = [self makeImageLayer: imageRef fromSize: startsize toSize: endsize withOrientation: img_orientation];
// swap the old and the new layer
[CATransaction begin];
[CATransaction setValue: [NSNumber numberWithFloat: fading_duration] forKey: kCATransactionAnimationDuration ];
[mainLayer_ replaceSublayer: currentLayer_ with: newlayer];
currentLayer_ = newlayer;
[CATransaction commit];
}
____________________________________________________________
Der Liebende ist nicht blind, sondern sieht mehr.
____________________________________________________________
http://zach.in.tu-clausthal.de
____________________________________________________________
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden