You can use flash to play an animation when the App starts. You can also use multiple consecutive images. In the project, I chose the latter.
Through the continuous animation effects of multiple images, the system's built-in UIImageView can complete this function. I did this at the beginning, but finally found that the memory burst, more than MB (iPAD ). (Note: a k png Image is initialized to occupy several MB to dozens of MB after it is put into memory)
Finally, I chose to refresh UIImageView. image through the timer.
Here I was fooled by the system. [UIImgae imageName:] and [UIImage imageWithContentsOfFile:]. In theory, the two methods are: the system allocates a memory cache image and remains in the app throughout its lifecycle, the latter is temporarily stored in the memory and will be released afterwards. When I used the latter, I found that the memory burst, and it seemed that the system did not release the memory. This problem has plagued me for a long time. How can we make the system release the space in time? from another perspective, it may be better to manually apply for it-manually release it.
So I replaced it with the [UIImage alloc] initWIthContentsOfFile:] method, which successfully solved the problem that the memory cannot be released. I have more than 106 animated images. In the test, I found that only 40-50 m space is available.
To solve the memory problem, it is imperative to make images refresh quickly.
I have created a cache pool, and the background asynchronously reads the image to NSMutiableArray. The main thread obtains the image from the array and regularly refreshes It To The ImageView. This method improves the performance of multi-core devices and provides smoother animation.
The following is the core code:
[Cpp]
-(Void) precache
{
_ CacheImages = TRUE;
_ CacheImageArray = [[NSMutableArray alloc] initWithCapacity: 0];
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
NSLog (@ "################################ image swap begin # ########################");
UIImage * img = nil;
For (int I = 1; I <[_ imageNames count]; I ++)
{
If (_ cacheImageArray. count <= KSwapImageNum ){
NSString * name = [_ imageNames objectAtIndex: I];
Img = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: nil];
[_ CacheImageArray addObject: img];
[Img release]; img = nil;
} Else {
[_ RequestCondition lock];
[_ RequestCondition wait];
[_ RequestCondition unlock];
I --;
}
}
NSLog (@ "################################ image swap end # ########################");
});
}
-(Void) setImageAtIndex :( NSInteger) index
{
_ Imageset = TRUE;
NSString * name = [_ imageNames objectAtIndex: index];
// Load the image from the bundle
UIImage * img = nil;
If (_ cacheImages)
{
If (_ cacheImageArray. count> 0 ){
Img = [_ cacheImageArray objectAtIndex: 0];
// Set it into the view
_ ImageView. image = nil;
[_ ImageView setImage: img];
[_ CacheImageArray removeObjectAtIndex: 0];
If (_ cacheImageArray. count <= KSwapImageMinNum ){
[_ RequestCondition signal];
}
Img = nil;
}
}
Else
{
Img = [[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: name ofType: nil];
// Set it into the view
[_ ImageView setImage: img];
[Img release]; img = nil;
}
}
-(Void) precache
{
_ CacheImages = TRUE;
_ CacheImageArray = [[NSMutableArray alloc] initWithCapacity: 0];
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
NSLog (@ "################################ image swap begin # ########################");
UIImage * img = nil;
For (int I = 1; I <[_ imageNames count]; I ++)
{
If (_ cacheImageArray. count <= KSwapImageNum ){
NSString * name = [_ imageNames objectAtIndex: I];
Img = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: nil];
[_ CacheImageArray addObject: img];
[Img release]; img = nil;
} Else {
[_ RequestCondition lock];
[_ RequestCondition wait];
[_ RequestCondition unlock];
I --;
}
}
NSLog (@ "################################ image swap end # ########################");
});
}
-(Void) setImageAtIndex :( NSInteger) index
{
_ Imageset = TRUE;
NSString * name = [_ imageNames objectAtIndex: index];
// Load the image from the bundle
UIImage * img = nil;
If (_ cacheImages)
{
If (_ cacheImageArray. count> 0 ){
Img = [_ cacheImageArray objectAtIndex: 0];
// Set it into the view
_ ImageView. image = nil;
[_ ImageView setImage: img];
[_ CacheImageArray removeObjectAtIndex: 0];
If (_ cacheImageArray. count <= KSwapImageMinNum ){
[_ RequestCondition signal];
}
Img = nil;
}
}
Else
{
Img = [[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: name ofType: nil];
// Set it into the view
[_ ImageView setImage: img];
[Img release]; img = nil;
}
}