Ios 如何从NSArray中随机选取图像并在视图中随机显示

Ios 如何从NSArray中随机选取图像并在视图中随机显示,ios,objective-c,xcode,image,arc4random,Ios,Objective C,Xcode,Image,Arc4random,好吧,我已经做了几个线程,询问如何随机显示图像/选择一个随机图像。我意识到我不知道如何将这两种方法结合起来 我在一个数组中有5个图像 我的.h文件: @property (strong, nonatomic) NSArray *imageArray; 我的.m文件: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. UII

好吧,我已经做了几个线程,询问如何随机显示图像/选择一个随机图像。我意识到我不知道如何将这两种方法结合起来

我在一个数组中有5个图像

我的.h文件:

@property (strong, nonatomic) NSArray *imageArray;
我的.m文件:

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];

_imageArray = @[image1, image2, image3, image4, image5];
}
这就是我现在所拥有的,我一直在玩弄我在别处找到的其他代码,但没有成功,所以就把它忽略了

现在你看到了我所拥有的,这就是我想要做的: 我需要使用一种方法,从我的数组中随机选取5幅图像中的一幅,然后在视图中的随机位置显示它

我还需要重复这个循环,但有一些限制。我需要每个图像都有一个“值”,例如:image-1等于1,image-2等于2,image-3等于3,image-4等于4,image-5等于5。并重复循环,直到显示的图像总数等于50

我真的不知道从哪里开始使用什么方法。我相信随机选择和显示对你们中的一些人来说很容易,但是值和重复直到等于50似乎很复杂。因此,非常感谢您的任何帮助!提前感谢任何能提供帮助的人,我是新来的编码,所以如果你能解释为什么你使用你的代码,它会帮助更多!谢谢


编辑:这家伙试图在我的另一个线程中帮助我添加整个过程,并选择一个随机图像。他的回答是:我使用了他的代码,但什么也没发生。我不确定他的代码是哪里出错了,还是我做错了什么。

此代码使用github上可用的M42randomindexpermution类:

int main(int argc,const char*argv[]
{
@自动释放池{
NSArray*图像=@[“图像-1”、“图像-2”、“图像-3”、“图像-4”、“图像-5];
M42RandomIndexPermutation*permutation=[[M42RandomIndexPermutation alloc]initWithCount:images.count usingSeed:[NSDate date].TimeIntervalences1970];
对于(int i=0;i,这可能会有帮助:

int index = arc4random % ([_imageArray count] - 1);

imageView.image = [_imageArray objectAtIndex:index];

干杯!

使用一个递归循环和一个int来跟踪您向所需的50计数的进度

每次通过循环时,您都希望:

  • 生成新的随机数(0-4)
  • 使用随机数从阵列中选择图像
  • 把你的随机数加到整数上,检查你是否达到50
  • 如果你已经50岁了,你就完了
  • 如果你还没有达到50岁,那就再做一次
  • 大概是这样的:

    //in your .h declare an int to track your progress
    int myImgCount;
    
    //in your .m
    -(void)randomizeImages {
    
      //get random number
      int randomImgNum = arc4random_uniform(5);  
    
      //use your random number to get an image from your array
      UIImage *tempImg = [_imageArray objeactAtIndex:randomImgNum];
    
      //add your UIImage to a UIImageView and place it on screen somewhere
      UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];  
    
      //define the center points you want to use
      tempImgView.center = CGPointMake(yourDesiredX,yourDesiredY);   
    
      [self addSubview:tempImgView];
      [tempImgView release];
    
    
      //increment your count
      myImgCount = myImgCount+(randomImgNum+1); 
    
      //check your count
      if (myImgCount<50) {
        [self randomizeImages];  //do it again if not yet at 50
      }
    
    }
    
    //在.h中声明一个int以跟踪进度
    int myImgCount;
    //在你的房间里
    -(无效)随机化图像{
    //获取随机数
    int randomImgNum=arc4random_均匀(5);
    //使用随机数从阵列中获取图像
    UIImage*tempImg=[\u imageArray objeactAtIndex:randomImgNum];
    //将UIImage添加到UIImageView并将其放置在屏幕上的某个位置
    UIImageView*tempImgView=[[UIImageView alloc]initWithImage:tempImg];
    //定义要使用的中心点
    tempImgView.center=CGPointMake(yourdiredx,yourdiredy);
    [自添加子视图:tempImgView];
    [tempImgView发布];
    //增加你的计数
    myImgCount=myImgCount+(随机imgnum+1);
    //检查一下你的计数
    
    if(MyImgCount构建一个数组,然后对其进行每一次变异。我将为您编写示例代码我不确定每一次变异意味着什么,但示例代码会有所帮助!置换。自动更正添加了空格“以不同的顺序排列(项)”;)重点是我有一个数组,我没有随机化,但我正在随机化要使用的索引我不明白为什么在代码的后半部分使用日期和时间?我尝试过类似于前半部分的东西,它只是在随机位置显示我的所有图像一次,与第一个代码相同,但没有重复..所以总是2 1 3 4 5或5 3 1 2 4或soit基于种子构建排列。Arc4 Random也是,但它使用隐式种子使用
    Arc4 Random\u uniform
    ,而不是
    Arc4 Random
    。函数的统一版本将保证值的均匀分布。否则,您将有偏差。除此之外,答案很棒。这是answer是有意义的,我理解发生了什么,但我不知道为什么我不能在我的视图上显示图像!有什么建议吗?你正在调用该方法吗?从你的viewDidLoad方法,你需要调用[self randomizeImages]来启动它。你还需要实际设置(或随机化?)你所有UIImageView的中心点..谢谢Neal的提示。在尝试帮助时总是喜欢学习一些东西。我会编辑我的答案以反映。非常感谢你!你不知道我为此奋斗了多久!你是我的英雄!还有,你的代码int randoimgnum=arc4random_uniform()%5;给出了一个错误,当我这样说时它起作用了:int randomImgNum=arc4random_uniform(5);只是让您知道。
    //in your .h declare an int to track your progress
    int myImgCount;
    
    //in your .m
    -(void)randomizeImages {
    
      //get random number
      int randomImgNum = arc4random_uniform(5);  
    
      //use your random number to get an image from your array
      UIImage *tempImg = [_imageArray objeactAtIndex:randomImgNum];
    
      //add your UIImage to a UIImageView and place it on screen somewhere
      UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];  
    
      //define the center points you want to use
      tempImgView.center = CGPointMake(yourDesiredX,yourDesiredY);   
    
      [self addSubview:tempImgView];
      [tempImgView release];
    
    
      //increment your count
      myImgCount = myImgCount+(randomImgNum+1); 
    
      //check your count
      if (myImgCount<50) {
        [self randomizeImages];  //do it again if not yet at 50
      }
    
    }