IPhone-updating an array on button click
I am new to IPhone development.
When I click on the button programmatically a new image in an IMAGEVIEW is displayed on the UIView. I want to store the images in some container and then use them later.
Here is my source code for creating images through UIGraphics.
if ([shape isEqualToString:@"Rectangle"])
{
UIGraphicsBeginImageContext(CGSizeMake(100.0, 100.0));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGRect main =CGRectMake(15.0, 15.0, 70.0, 30.0);
CGContextFillRect(context, main);
CGRect topSeat1 = CGRectMake(15.0, 0.0, 15.0, 13.0);
CGRect topSeat2 = CGRectMake(42.5, 0.0, 15.0, 13.0);
CGRect topSeat3 = CGRectMake(70.0, 0.0, 15.0, 13.0);
CGRect leftSeat = CGRectMake(0.0, 22.5, 13.0, 15.0);
CGRect rightSeat = CGRectMake(87.0, 22.5, 13.0, 15.0);
[[UIColor redColor]set];
//UIRectFill(main);
UIRectFill(topSeat1);
UIRectFill(topSeat2);
UIRectFill(topSeat3);
UIRectFill(leftSeat);
UIRectFill(rightSeat);
UIRectFrame(main);
[[UIColor blackColor]set];
UIRectFrame(topSeat1);
UIRectFrame(topSeat2);
UIRectFrame(topSeat3);
UIRectFrame(leftSeat);
UIRectFrame(rightSeat);
UIImage * image = [[UIImage alloc]init];
image = UIGraphicsGetImageFromCurrentImageContext();
myImage=[[UIImageView alloc]initWithImage:image];
UIGraphicsEndImageContext();
[self.view addSubview:myImage];
[myImage setUserInteractionEnabled:YES];
[arrayOfImages addObject:myImage];
}
However the image is displayed but when I again click on the button, another images gets displayed but the older one on the UIView becomes static and I can't do anything to it.
I would 开发者_运维问答prefer some code.
All I ask is:
How to store them, so that on every button click the container gets populated or updated with UIImageViews.
How to restore them later.
I probably did not understand your question completely, if you just want to store the images for reference at a later point of time you can add them to an array.
NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
[imagesArray addObject: image];
If you would like to access the images later, you can do it through the index
[imagesArray objectAtIndex: indexOfTheImage];
I would do it like this:
- (IBAction)button {
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(160, 240, 40, 40)];
[image setImage:@"yourimage.png"];
[self.view addSubview:image];
[myArray addObject:image];
}
You'l have to use: [imagesArray objectAtIndex: indexOfTheImage];
As the 1st answer says.
hope i helped :)
精彩评论