I’ve been troubleshooting a project that animates 24 different sets of png images. Each set containing around 30 images. The App would eventually crash when run on an iPad after running through about half a dozen of the animations. Eventually running into a memory limit. In my search for solutions, I ran into Mo DeJong’s PNGAnimatorDemo, which completely solved the memory limit problem.
The example project he provides runs the animation full frame by default so I had to modify some things in order to add it as a subview to another view and to set it inside a CGRect.
The first thing to be done was to create a new property ‘viewCGRect’ with which I could set a CGRect for the ImageAnimatorViewController . I also took out the animationOrientation property and associated code since this is now being added as a subVIew to another view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (void)loadView { // UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; UIView *myView = [[UIView alloc] initWithFrame:viewCGRect]; [myView autorelease]; self.view = myView; // FIXME: Additional Supported Orientations if (animationOrientation == UIImageOrientationUp) { // No-op } else if (animationOrientation == UIImageOrientationLeft) { // 90 deg CCW [self rotateToLandscape]; } else if (animationOrientation == UIImageOrientationRight) { // 90 deg CW [self rotateToLandscapeRight]; } else { NSAssert(FALSE,@"Unsupported animationOrientation"); } |
Then inside my superViewController I modified the startAnimator method in order to pass a different file name prefix for every animation. As well as a specific length (each animation had varying lengths) and a CGRect with which to draw the animation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
- (void) startAnimator:(NSString *) pref endingWith: (int) end andFrame:(CGRect) rect { //[viewController.view removeFromSuperview]; self.animatorViewController = [ImageAnimatorViewController imageAnimatorViewController]; NSArray *names = [ImageAnimatorViewController arrayWithNumberedNames:pref rangeStart:1 rangeEnd:end suffixFormat:@"%02i.png"]; NSArray *URLs = [ImageAnimatorViewController arrayWithResourcePrefixedURLs:names]; // setting the public property for our custom CGRect: animatorViewController.viewCGRect = rect; // we no longer need to provide an orientation //animatorViewController.animationOrientation = UIImageOrientationUp; // Rotate 90 deg CCW animatorViewController.animationFrameDuration = ImageAnimator15FPS; animatorViewController.animationURLs = URLs; animatorViewController.animationRepeatCount = 0; // Show animator before starting animation // add the animatorViewController.view as a subView: [self.view addSubview:animatorViewController.view]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(animationDidStartNotification:) name:ImageAnimatorDidStartNotification object:animatorViewController]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(animationDidStopNotification:) name:ImageAnimatorDidStopNotification object:animatorViewController]; [animatorViewController startAnimating]; } [/cc] Then lastly I commented out parts of the stopAnimator method so that the final frame of each animation would remain until I needed to dispose of it by using another method clearAnimation. [cc lang="objc"] - (void) stopAnimator { [[NSNotificationCenter defaultCenter] removeObserver:self name:ImageAnimatorDidStartNotification object:animatorViewController]; [[NSNotificationCenter defaultCenter] removeObserver:self name:ImageAnimatorDidStopNotification object:animatorViewController]; [animatorViewController stopAnimating]; // [animatorViewController.view removeFromSuperview]; // self.animatorViewController = nil; } - (void) clearAnimation { [animatorViewController.view removeFromSuperview]; self.animatorViewController = nil; } |