0

Alright, so I have 5 custom images total. Heres the values I need to set each image to:

Image1 = 1
Image2 = 2
Image3 = 3
Image4 = 4
Image5 = 5

I need values assigned to these because I want to have xcode randomly place them on the view until it reaches a value of 50. So im assuming I need some kind of loop that adds up the values until 50 is reached?

How do I assign values to these images since it brings up a warning when trying to assign an int value to a UIImage. Also, on a side note what method would I use to randomly place the images on the view without overlapping?

Thanks for any and all help!

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
iann
  • 7
  • 1
  • 4
  • not sure whether i got your question properly. if you have control on image names you can name it with suffix or prefix 1,2,3, until 50. then use an array and create random number like this http://stackoverflow.com/questions/1617630/non-repeating-random-numbers – thndrkiss Oct 07 '13 at 22:53
  • It is always amusing to see a question put on hold for "unclear what you're asking" when it gets multiple answers and the user's issue solved. – HalR Oct 12 '13 at 00:07

3 Answers3

3

Your app will be placing UIImageViews, not UIImages onto a view. Like all UIView subclasses, UIImageView has an NSInteger tag property, but if I understand the problem correctly, I don't think you need that, either.

// add count randomly selected images to random positions on self.view
// (assumes self is a kind of UIViewController)
- (void)placeRandomImages:(NSInteger)count {

    for (NSInteger i=0; i<count; ++i) {
        UIImage *image = [self randomImage];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = [self randomFrameForImage:image];
        [self.view addSubview:imageView];

        // add a tag here, if you want, but I'm not sure what for
        // imageView.tag = i;
    }
}

// answer a random image from the app's bundle
// assumes the images are named image-x where x = 0..4
- (UIImage *)randomImage {

    NSInteger imageNumber = arc4random() % 5;
    NSString *imageName = [NSString stringWithFormat:@"image-%d", imageNumber];
    return [UIImage imageNamed:imageName];
}

// answer a random position for the passed image, keeping it inside the view bounds
- (CGRect)randomFrameForImage:(UIImage *)image {

    CGFloat imageWidth = image.width;
    CGFloat imageHeight = image.height;

    CGFloat maxX = CGRectGetMaxX(self.view.bounds) - imageWidth;
    CGFloat maxY = CGRectGetMaxY(self.view.bounds) - imageHeight;

    // random location, but always inside my view bounds
    CGFloat x = arc4random() % (NSInteger)maxX;
    CGFloat y = arc4random() % (NSInteger)maxY;

    return CGRectMake(x,y,imageWidth,imageHeight);
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • +1 for multiple implementations. – Kyle C Oct 07 '13 at 22:59
  • Use `arc4random_uniform()` to avoid modulo bias. – danielbeard Oct 07 '13 at 22:59
  • Hey danh, was looking for a messaging feature to ask you about this but I dont think there is one. In your line of code UIImageView *imageView = [UIImageView imageViewWithImage:image]; image.frame = [self randomFrameForImage:image]; the imageViewWithImage part xcode says "no known class method for selector". and below that image.frame it says "property 'frame' not found on object of type 'UIImage'" any info to clear this up would be appreciated – iann Oct 11 '13 at 20:31
  • Sorry about that. The correct form is [[UIImageView alloc] initWithImage:image]. I will edit. – danh Oct 11 '13 at 21:39
  • Thanks for helping on that first part! Its still having problems with frame. Do I need to declare frame as a new UIImageView in the .h file? – iann Oct 11 '13 at 22:33
  • Ugh. Sorry. Didn't read that. frame is a property of the imageView. Will edit. – danh Oct 11 '13 at 23:53
  • Thanks! Im surprised I didn't find that when I searched everywhere. One last question if youre not tired of me yet lol. Do my images have to be in an array? Or do I need to add any of that code to .h? Because nothing happened lol – iann Oct 12 '13 at 01:12
  • No no. Sorry for the coding errors. I should have tried first in Xcode rather than coding from memory. The approach I suggest should work if you just add a set of images to your app bundle (just drag them in) named image-0.png, image-1.png, ... etc. – danh Oct 12 '13 at 02:26
2

If want to assign an arbitrary integer value, I would use the tag property on UIImageView.

 NSInteger currentTag = 50;
 while (currentTag > 0) {
        UIImageView *imageView = [UIImageView alloc initWithImage:image];
         imageView.tag = currentTag;
         [self.view addSubView:imageView];
         currentTag--;
   }
Kyle C
  • 4,077
  • 2
  • 31
  • 34
0

Put the images into an NSArray then from NSHipster:

How Do I Pick a Random Element from an NSArray

Use arc4random_uniform(3) to generate a random number in the range of a non-empty array.

if ([array count] > 0) {
  id obj = array[arc4random_uniform([array count])];
}
JuJoDi
  • 14,627
  • 23
  • 80
  • 126