Array of [Images and CGPoints]

Discussion in 'iOS Development' started by JoshuaCaputo, Dec 18, 2008.

  1. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    Hello,
    I want to make an array of 100 images (all the same) and 100 CGPoints (different)

    How can I do this?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I would use a standard C style array:
    Code:
    UIImageView *imageView[100];
    int i = 0;
    for (i = 0; i <= 99; i++){
    imageView[i] = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]] autorelease];
    }
    //Refer to your images with a variable, or a static int, starting with 0
    //imageView[1]...  imageView[54]... imageView[x]... etc.
    
    Most people here don't like that (although it is considerably easier in this case), so they use an Objective-C style array:
    Code:
    NSMutableArray *imageView = [[NSMutableArray alloc] init];
    int i = 0;
    for (i = 0; i <= 99; i++){
    [imageView addObject:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]] autorelease]];
    //Refer to your images with a variable, or a static int, starting with 0
    //[imageView objectAtIndex:1]...  [imageView objectAtIndex:54]...  [imageView objectAtIndex:x]...
    }

    EDIT: Do the exact same thing for your CGPoints. A convenient way to combine the two arrays into one would be to use a C style array and make it two dimensional.
  3. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    like i know how to do that.

    THANKS
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    You could also make an NSArray of CGPoints (assume you have an NSMutableArray)

    Code:
    [myArray addObject: NSStringFromCGPoint(myPoint)];
    You can retrieve the point structure as follows (i being the index):
    Code:
    CGPoint newPoint = CGPointFromString((NSString*)[myArray objectAtIndex: i])
    The function names are maybe a bit off; I just skimmed through the SDK documentation a few days ago and found these, but possibly didn't remember them correctly.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    It's still easier to just make a simple array:
    Code:
    CGPoint *point[100];
    Then just refer to your points as
    Code:
    point[number];
  6. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    I like skylers way. more like javascript which is what I learned first
  7. veeloc

    veeloc New Member

    Joined:
    Sep 6, 2008
    Messages:
    815
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    ive read this over a couple times and i still don't know... is it sarcastic???

    anyway yeah the C way is better, more logical
  8. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    I REALLY STILL DONT UNDERSTAND! MAYBE Some source code would help?

    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	UIImageView *imageView[100];
    	int i = 0;
    	for (i = 0; i <= 99; i++){
    		imageView[i] = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red.png"]] autorelease];
    	}
    	//Refer to your images with a variable, or a static int, starting with 0
    	//imageView[1]...  imageView[54]... imageView[x]... etc.
    	imageView[99].center = CGPointMake(100,100);
    	imageView[1].frame = CGRectMake(20, 20, 49, 49);
    }
    =nothing displays?
  9. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Your not adding it to the view.
    Code:
    - (void)awakeFromNib {
    	UIImageView *imageView[100];
    	int i = 0;
    	for (i = 0; i <= 99; i++){
    		imageView[i] = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red.png"]] autorelease];
                    [self addSubView:imageView[i]];
    	}
    	//Refer to your images with a variable, or a static int, starting with 0
    	//imageView[1]...  imageView[54]... imageView[x]... etc.
    	imageView[99].center = CGPointMake(100,100);
    	imageView[1].frame = CGRectMake(20, 20, 49, 49);
    }
    
    I tried this and it worked. Not in a ViewController though, it's in a UIView. I think
    Code:
    [self.view addSubview:imageView[i]]; 
    would work though.
  10. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    OMG, how did i miss that haha

Share This Page