[英]Loop through array contents' names
我想顯示一個.gif(就像自動播放視頻一樣),因此我發現我需要使用UIImages
數組。 現在,我有一個包含96張圖像的視頻,因此我想使用一個循環來命名它們。 那可能嗎?
我嘗試了以下操作,但不幸的是它不起作用:
for (int i = 0; i<97; i++) {
[UIImage imageNamed:@"%d", i];
}
因此,我想對循環執行以下操作:
NSArray *myArray = [NSArray arrayWithObjects:
for (int i = 0; i<97; i++) {
[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
}
, nil];
那么有沒有辦法在數組中使用for循環? 有什么想法嗎?
我不確定您到底想要什么,但是如果要使用一批圖像顯示動畫,則可以使用以下方法。
// 1. Create an array of images names. If you have images named 0.png, 1.png, ... 95.png:
NSMutableArray *imageNames = [NSMutableArray new];
for (int i = 0; i < 96; i++)
{
[imageNames addObject:[NSString stringWithFormat:@"%d", i]];
}
// 2. Create an array of images from array of names:
NSMutableArray *images = [[NSMutableArray alloc] init];
[imageNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[images addObject:[UIImage imageNamed:obj]];
}];
// 3. Assign it to UIImageView:
self.imageView = [UIImageView new];
[self.view addSubview: self.imageView];
self.imageView.animationImages = images;
self.imageView.animationDuration = 0.5;
[self.imageView startAnimating];
您可以在此鏈接找到更多
imagview有一個屬性,即animationImages。 您可以將arrayNames添加到此屬性。然后它起作用
在.h文件中創建一個如下所示的屬性:
@property(nonatomic, strong) NSMutableArray *images;
像這樣在viewDidLoad方法中初始化它:
- (void)viewDidLoad
{
[super viewDidLoad];
self.images = [[NSMutableArray alloc] init];
}
您需要執行以下操作:
for (int i = 0; i<97; i++)
{
UIImage *currentImage = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[self.images addObject:currentImage];// images is a NSMutableArray
}
如果要為這些圖像設置動畫,可以執行以下操作(假設您有一個名稱為imageView的UIImageView對象):
imageView.animationImages = self.images;
[imageView startAnimating];
瀏覽UIImageView類文檔以獲取更多選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.