我将10个图像存储在数组中,如下所示。
images=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];
<。>文件中的
NSMutableArray *images;
因为我在数组中存储了10个图像。从这里我需要选择随机的6个图像并需要在UIImageView中显示。
在UIViewController中,我已经放置了6个UIImageView和连接,如下所示。
@property(weak,nonatomic)IBOutlet UIImageView *b1;
@property(weak,nonatomic)IBOutlet UIImageView *b2;
@property(weak,nonatomic)IBOutlet UIImageView *b3;
@property(weak,nonatomic)IBOutlet UIImageView *b4;
@property(weak,nonatomic)IBOutlet UIImageView *b5;
@property(weak,nonatomic)IBOutlet UIImageView *b6;
那该怎么办?
答案 0 :(得分:1)
简单解决方案,试试这个
NSUInteger randomIndexValue = arc4random() % [images count];
答案 1 :(得分:0)
你需要先找到数组的随机索引。然后你可以访问索引数据。这里我是swift的示例代码。
let randomIndex = arc4random() % 10
let image = imageArray[randomIndex]
现在您可以将图像用于imageview。希望你能把它转换成目标c。
答案 2 :(得分:0)
-(NSMutableArray*)getRandomValueFromArray:(NSMutableArray*)arrAllData randomDataCount:(NSInteger)count {
NSMutableArray *arrFilterData = [[NSMutableArray alloc]init];
for(int i=0; i<count; i++){
NSInteger index = arc4random() % (NSUInteger)(yourArray.count);
[arrFilterData addObject:[arrAllData objectAtIndex:index]];
[yourArray removeObjectAtIndex:index];
}
return arrFilterData;
}
自己计算。
答案 3 :(得分:0)
实际上你有两次5张图片,所以你的imageViews至少要有一倍。
static NSArray *imagenames = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imagenames = @[
@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg",
@"Christmas-Wallpapers-HD-Picture.jpg",
@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg",
@"tree.jpg",
@"luxury-christmas-napkins-father-christmas-1635-p.jpg",
@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg",
@"Christmas-Wallpapers-HD-Picture.jpg",
@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg",
@"tree.jpg",
@"luxury-christmas-napkins-father-christmas-1635-p.jpg"];
});
NSArray<UIImageView *> *imageViews = @[self.b1, self.b2, self.b3, self.b4, self.b5, self.b6];
NSMutableArray * tmpnames = imagenames.mutableCopy;
for (UIImageView *imageView in imageViews) {
NSUInteger randomIndex = arc4random_uniform((uint32_t)tmpnames.count);
imageView.image = [UIImage imageNamed:tmpnames[randomIndex]];
[tmpnames removeObjectAtIndex:randomIndex];
}