如何在iphone中随机显示25张图像中的(5张、7张或8张等)图像,而不重复阵列

如何在iphone中随机显示25张图像中的(5张、7张或8张等)图像,而不重复阵列,iphone,Iphone,在myFirstViewcontroller中,我有一个包含1到25值的选择器,如果用户在下一个屏幕的选择器中选择5,它应该在25个图像中随机显示5个图像,而不重复这5个图像。若用户选择另一个数字,则表示它将该数字作为输入,并应根据该数字显示图像。如果我运行我的代码,意味着每次它都会显示所有25个图像,而不是选定的数字图像。这是我的密码 - (void)viewDidLoad { myImageNames = [[NSMutableArray alloc] init];

在my
FirstViewcontroller
中,我有一个包含
1
25
值的选择器,如果用户在下一个屏幕的选择器中选择5,它应该在25个图像中随机显示5个图像,而不重复这5个图像。若用户选择另一个数字,则表示它将该数字作为输入,并应根据该数字显示图像。如果我运行我的代码,意味着每次它都会显示所有25个图像,而不是选定的数字图像。这是我的密码

- (void)viewDidLoad {    
    myImageNames = [[NSMutableArray alloc] init];

    [myImageNames addObject:[UIImage imageNamed:@"Red.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"Blue.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"LightRed.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"Orange.png"]];
    .
    .
    .

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(ChangingImgView:) userInfo:nil repeats:YES];
}

- (void) ChangingImgView:(int)selectedNumberFromPicker {
    selectedNumberFromPicker = [num intValue];

    for (int i=0; i < selectedNumberFromPicker; i++) {

        index = arc4random() % 25;
        NSString *strIndex = [NSString stringWithFormat:@"%i",index];
        [myImageNames addObject:strIndex];
        imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        imgBtn.frame = CGRectMake(250, 300, 170, 220);
        UIImage *img = [myImageNames objectAtIndex:index];
        [imgBtn setBackgroundImage:img forState:UIControlStateNormal];
        [imgBtn addTarget:self action:@selector(ClickingOnImage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:imgBtn];
    }
}
-(void)viewDidLoad{
myImageNames=[[NSMutableArray alloc]init];
[myImageNames添加对象:[UIImage ImageName:@“Red.png”];
[myImageNames添加对象:[UIImageName:@“Blue.png”];
[myImageNames添加对象:[UIImageName:@“LightRed.png”];
[myImageNames添加对象:[UIImage ImageName:@“Orange.png”];
.
.
.
计时器=[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(ChangingImgView:)用户信息:无重复:是];
}
-(无效)更改imgView:(int)从选择器中选择数字{
selectedNumberFromPicker=[num intValue];
for(int i=0;i

请帮帮我。

首先
使用链接将您的
myImageNames
包含
images


现在
显示
前5张
图像
来自
myImageNames

首先
suffle
您的
myImageNames
,其中包含
图像


现在,从
myImageNames
中显示前5张
images
,您也可以使用NSSet而不是NSMutableArray

苹果医生


如果元素的顺序不重要,并且测试对象是否包含在集合中的性能是一个考虑因素,则可以使用集合作为数组的替代。当数组排序时,测试成员资格比使用集合慢。

您也可以使用NSSet而不是使用NSMUTABLEARRY

int count = 5;
NSMutableArray *inputImages = [myImageNames mutableCopy];
NSMutableArray *randomImages;

for (int i = 0; i<count; i++) {
    int index = arc4random_uniform([inputImages count]);
    [randomImages addObject:[inputImages objectAtIndex:index]];
    [inputImages removeObjectAtIndex:index];
}
苹果医生

如果元素的顺序不重要,并且测试对象是否包含在集合中的性能是一个考虑因素,则可以使用集合作为数组的替代。当数组排序时,测试成员资格比使用集合慢。
int count=5;
int count = 5;
NSMutableArray *inputImages = [myImageNames mutableCopy];
NSMutableArray *randomImages;

for (int i = 0; i<count; i++) {
    int index = arc4random_uniform([inputImages count]);
    [randomImages addObject:[inputImages objectAtIndex:index]];
    [inputImages removeObjectAtIndex:index];
}
NSMutableArray*inputImages=[myImageNames mutableCopy]; NSMutableArray*随机图像; 对于(int i=0;i
int count=5;
NSMutableArray*inputImages=[myImageNames mutableCopy];
NSMutableArray*随机图像;

for(int i=0;i尝试使用此for循环获取随机索引的特定数量的图像。使用selectedImgArray显示所选图像。myImageNames是原始数组

NSInteger index;
NSMutableArray *selectedImgArray = [[NSMutableArray alloc] init];
for (int i=0; i < selectedNumberFromPicker; i++) {
    index = arc4random() % 25;
    UIImage *selImg = (UIImage *)[myImageNames objectAtIndex:index];
    if ([selectedImgArray containsObject:selImg]) {
        i--;
        continue;
    }
    [selectedImgArray addObject:selImg];

}
NSInteger索引;
NSMutableArray*selectedImgArray=[[NSMutableArray alloc]init];
for(int i=0;i
尝试使用此for循环获取随机索引的特定数量的图像。使用selectedImgArray显示所选图像。myImageNames是原始数组

NSInteger index;
NSMutableArray *selectedImgArray = [[NSMutableArray alloc] init];
for (int i=0; i < selectedNumberFromPicker; i++) {
    index = arc4random() % 25;
    UIImage *selImg = (UIImage *)[myImageNames objectAtIndex:index];
    if ([selectedImgArray containsObject:selImg]) {
        i--;
        continue;
    }
    [selectedImgArray addObject:selImg];

}
NSInteger索引;
NSMutableArray*selectedImgArray=[[NSMutableArray alloc]init];
for(int i=0;i
感谢您的回复,您能给我简要解释一下吗感谢您的回复,您能给我简要解释一下吗?只需制作所有图像的数组,然后逐个删除对象…您将得到您的结果:)只需制作所有图像的数组,然后逐个删除对象…您将得到您的结果:)