UIImage animation, causing the application to crash / leak memory

I use UIImage animations, and this causes numerous memory leaks and crashes for different users using the application.

Below is my code. I preload a set of two animations in viewDidAppear

pointsView.image = [UIImage imageNamed: @ "C72.png"];

NSMutableArray *menuanimationImages = [[NSMutableArray alloc] initWithCapacity:21]; NSString *imageName; for( int aniCount = 0; aniCount < 72; aniCount++ ) { imageName = [NSString stringWithFormat:@"C%d.png", aniCount]; [menuanimationImages addObject:[UIImage imageNamed:imageName]]; } pointsView.animationImages = menuanimationImages; pointsView2.image = [UIImage imageNamed:@"I72.png"]; NSMutableArray *menuanimationImagess = [[NSMutableArray alloc] initWithCapacity:21]; NSString *imageNames; for( int aniCounts = 0; aniCounts < 72; aniCounts++ ) { imageNames = [NSString stringWithFormat:@"I%d.png", aniCounts]; [menuanimationImagess addObject:[UIImage imageNamed:imageNames]]; } pointsView2.animationImages = menuanimationImagess; } 

Then I run the animation with

 pointsView.animationDuration = 3.11; pointsView.animationRepeatCount = 1; [pointsView startAnimating]; 

Any suggestions?

+1
source share
2 answers

Please read my blog post about this topic: video-and-memory-usage-on-ios-devices . The root of the problem is that you simply cannot simultaneously load these many images into main memory. You just do not need to use the UIImageView.animationImages API, it is badly broken and lures developers to write bad code that will crash when launched on the device.

+4
source

Do you immediately load 72 png images into memory? And depending on the size of these images, you could probably reach the memory limits of some older devices, forcing them to give a memory warning and ultimately crash. My suggestion is not to do 72 image animations. You can try to compress each image, which will reduce their quality and memory size, but loading 72 images for animation is simply not good in the first place.

0
source

Source: https://habr.com/ru/post/1236029/


All Articles