在UIImageView - iOS中加载动画图像

4 objective-c uiimageview uiimage nsarray ios

我有大约300张图像要加载动画,图像被命名 loading001.png, loading002.png, loading003.png, loading004.png………loading300.png

我是按照以下方式做的.

.h文件

    #import <UIKit/UIKit.h>
    @interface CenterViewController : UIViewController  {
        UIImageView *imgView;
    }

    @end
Run Code Online (Sandbox Code Playgroud)

.m文件

@implementation CenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    imgView = [[UIImageView alloc] init];
    imgView.animationImages = [[NSArray alloc] initWithObjects:
                               [UIImage imageNamed:@"loading001.png"],
                               [UIImage imageNamed:@"loading002.png"],
                               [UIImage imageNamed:@"loading003.png"],
                               [UIImage imageNamed:@"loading004.png"],
                               nil];
}

- (IBAction)startAnimation:(id)sender{
    [imgView startAnimating];
}

@end
Run Code Online (Sandbox Code Playgroud)

是否有一种将图像加载到数组中的有效方法.我曾尝试过,for loop但无法弄明白.

ico*_*ter 16

您可以尝试使用以下代码以更好的方式将图像加载到数组中

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray *imgListArray = [NSMutableArray array];
    for (int i=1; i <= 300; i++) {
        NSString *strImgeName = [NSString stringWithFormat:@"loading%03d.png", i];
        UIImage *image = [UIImage imageNamed:strImgeName];
            if (!image) {
                NSLog(@"Could not load image named: %@", strImgeName);
            }
            else {
                [imgListArray addObject:image];
            }
        }
    imgView = [[UIImageView alloc] init];
    [imgView setAnimationImages:imgListArray];
}
Run Code Online (Sandbox Code Playgroud)