Images in the project NOT adding to array - don't know why - iphone-sdk-3.0

I have to sets of images. Each set has 16 images. One set is called 0.png through to 15.png the other is a0.png through to a15.png.
In my app, it loads each one dependent on a variable (which by logging, I have proved it works)
here is the code
[MemoryManager sharedMemoryManager];
NSLog(#"THEME: %#", [MemoryManager sharedMemoryManager].themeName);
imageArray = [[NSMutableArray alloc] init];
if([MemoryManager sharedMemoryManager].themeName == #"hand"){
NSLog(#"Here 2");
[imageArray addObject:[UIImage imageNamed:#"0.png"]]; //
[imageArray addObject:[UIImage imageNamed:#"1.png"]];//1
[imageArray addObject:[UIImage imageNamed:#"2.png"]];//2
[imageArray addObject:[UIImage imageNamed:#"3.png"]];//3
[imageArray addObject:[UIImage imageNamed:#"4.png"]];//4
[imageArray addObject:[UIImage imageNamed:#"5.png"]];//5
[imageArray addObject:[UIImage imageNamed:#"6.png"]];//6
[imageArray addObject:[UIImage imageNamed:#"7.png"]];//7
[imageArray addObject:[UIImage imageNamed:#"8.png"]];//8
[imageArray addObject:[UIImage imageNamed:#"9.png"]];//9
[imageArray addObject:[UIImage imageNamed:#"10.png"]];//10
[imageArray addObject:[UIImage imageNamed:#"11.png"]];//11
[imageArray addObject:[UIImage imageNamed:#"12.png"]];//12
[imageArray addObject:[UIImage imageNamed:#"13.png"]];//13
[imageArray addObject:[UIImage imageNamed:#"14.png"]];//14
[imageArray addObject:[UIImage imageNamed:#"15.png"]];//15
}
if([MemoryManager sharedMemoryManager].themeName == #"letters"){
NSLog(#"Here 3");
//[imageArray removeAllObjects];
[imageArray addObject:[UIImage imageNamed:#"a0.png"]]; //
[imageArray addObject:[UIImage imageNamed:#"a1.png"]];//1
[imageArray addObject:[UIImage imageNamed:#"a2.png"]];//2
[imageArray addObject:[UIImage imageNamed:#"a3.png"]];//3
[imageArray addObject:[UIImage imageNamed:#"a4.png"]];//4
[imageArray addObject:[UIImage imageNamed:#"a5.png"]];//5
[imageArray addObject:[UIImage imageNamed:#"a6.png"]];//6
[imageArray addObject:[UIImage imageNamed:#"a7.png"]];//7
[imageArray addObject:[UIImage imageNamed:#"a8.png"]];//8
[imageArray addObject:[UIImage imageNamed:#"a9.png"]];//9
[imageArray addObject:[UIImage imageNamed:#"a10.png"]];//10
[imageArray addObject:[UIImage imageNamed:#"a11.png"]];//11
[imageArray addObject:[UIImage imageNamed:#"a12.png"]];//12
[imageArray addObject:[UIImage imageNamed:#"a13.png"]];//13
[imageArray addObject:[UIImage imageNamed:#"a14.png"]];//14
[imageArray addObject:[UIImage imageNamed:#"a15.png"]];//15
NSLog(#"Here 4");
}
The log says
2010-05-26 21:30:57.092 Memory[22155:207] Here 1
2010-05-26 21:30:57.093 Memory[22155:207] THEME: letters
2010-05-26 21:30:57.095 Memory[22155:207] Here 3
2010-05-26 21:30:57.109 Memory[22155:207] Here 4
The images are in the same folder the .xproj file is. They simply is just not working.
Any ideas?
Cheers

I figured out. Long story short, I had forgot to add the if statements to another part of the code that happened during gameplay - thus it looked like it was not loading the images correctly, however the code was working just fine.
Ah one day.. xcode will be able to tell you what you are doing wrong sooner! i wish..

Related

UIBezierPath Multiple Line Colors

My attempt to draw UIBezierPath lines with different colors is failing me. All the lines change to the currently selected color. All my path and information are all stored in an NSMutableArray called pathInfo. In path info I drop in array that contains the Path, Color, Width, and Type of line. This works fine except all the lines turn to whatever color the user has selected. I would appreciate any help greatly!
- (void)drawRect:(CGRect)rect {
UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.miterLimit = 0;
for (int i = 0; i < [pathInfo count]; i++){
NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]];
NSLog(#"Path: %#",[row objectAtIndex:0]);
NSLog(#"Color: %#",[row objectAtIndex:1]);
NSLog(#"Width: %#",[row objectAtIndex:2]);
NSLog(#"Type: %#",[row objectAtIndex:3]);
//width
drawPath.lineWidth = [[row objectAtIndex:2] floatValue];
//color
[[row objectAtIndex:1] setStroke];
//path
[drawPath appendPath:[row objectAtIndex:0]];
}
UIBezierPath *path = [self pathForCurrentLine];
if (path)
[drawPath appendPath:path];
[drawPath stroke];
}
- (UIBezierPath*)pathForCurrentLine {
if (CGPointEqualToPoint(startPoint, CGPointZero) && CGPointEqualToPoint(endPoint, CGPointZero)){
return nil;
}
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
return path;
}
The stroke/fill colors only affect the -stroke command. They don't affect the -appendPath: command. Paths don't contain per-segment color information.
If you need a multi-colored line you're going to need to stroke each color separately.
Set your stroke color (and what have you), then stroke, and then move to the next path:
- (void)drawRect:(CGRect)rect
{
for (int i = 0; i < [pathInfo count]; i++){
NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]];
NSLog(#"Path: %#",[row objectAtIndex:0]);
NSLog(#"Color: %#",[row objectAtIndex:1]);
NSLog(#"Width: %#",[row objectAtIndex:2]);
NSLog(#"Type: %#",[row objectAtIndex:3]);
UIBezierPath *path = [row objectAtIndex:0];
path.lineCapStyle = kCGLineCapRound;
path.miterLimit = 0;
//width
path.lineWidth = [[row objectAtIndex:2] floatValue];
//color
[[row objectAtIndex:1] setStroke];
//path
[path stroke];
}
UIBezierPath *path = [self pathForCurrentLine];
if (path)
{
// set the width, color, etc, too, if you want
[path stroke];
}
}

setting the value to superview,when clicking the subviews

I have dynamically created uiviews,which having many buttons.It is for selecting particular value(uibutton) from each row(uiview).While clicking the button,I have to get the button image changed(like highlighting).Now I am getting like that,but order is not matching.Buttons are not getting selected in some row.please check the code below
for (int j=0; j<[viewTagAry count]; j++)
{
if ([[viewTagAry objectAtIndex:j] isEqualToString:[NSString stringWithFormat:#"%d",v]])
{
for (int i=0; i<[lastSelectedButtonAry count]; i++)
{
lastSelectedButton=[lastSelectedButtonAry objectAtIndex:i];
int tag=[[btnTagArray objectAtIndex:i] intValue];
UIImage *numImg=[UIImage imageNamed:[NSString stringWithFormat:#"%db.png",tag]];
[lastSelectedButton setBackgroundImage:numImg forState:UIControlStateNormal];
}
}
}
-(void)highlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
int t=button.tag+1;
UIImage *numImg=[UIImage imageNamed:[NSString stringWithFormat:#"%da.png",t]];
[button setBackgroundImage:numImg forState:UIControlStateNormal];
}

adding images on top of the button which is on a scrollview

NSData *data = [NSData dataWithContentsOfFile:#"path of XML"];
NSError *error = nil;
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
NSError *err=nil;
NSArray *nodes = [document nodesForXPath:#"/product_list/product[category = \"Pins & Collectibles\"]/image" error:&err];
NSMutableArray *array =[[NSMutableArray alloc]initWithCapacity:[nodes count]];
for(int i=0;i<[nodes count]; i++)
{
[array addObject:(NSString *)[[(NSString *)[[(NSString *)[[[NSString stringWithFormat:#"%#",[nodes objectAtIndex:i]] componentsSeparatedByString:#"{"] objectAtIndex:1] componentsSeparatedByString:#"<image>"] objectAtIndex:1] componentsSeparatedByString:#"</image>"] objectAtIndex:0] ];
}
NSLog(#"%#",array);
the array has all the images i need to put it on top of the button
This may work:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageWithData:IMAGE_DATA] forState:UIControlStateNormal];
Then make a for loop in that
for(int i=0;i<100;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageWithData:IMAGE_DATA] forState:UIControlStateNormal];
}

creating an images gallery

I m trying to create an image gallery where the images are stored in resource bundle. I'm storing my images in NSMutable array what i want is an image gallery ....but the output is pretty different from wat i expected.the below code works perfectly fine.to be more specific cud u guys help me out below is the code...
_images =[NSMutableArray arrayWithObject:[UIImage imageNamed:#"logo1.png"]];
[_images addObject:[UIImage imageNamed:#"logo2.png"]];
[_images addObject:[UIImage imageNamed:#"logo3.png"]];
[_images addObject:[UIImage imageNamed:#"logo4.png"]];
[_images addObject:[UIImage imageNamed:#"logo5.png"]];
[_images addObject:[UIImage imageNamed:#"logo6.png"]];
[_images addObject:[UIImage imageNamed:#"logo7.png"]];
[_images addObject:[UIImage imageNamed:#"logo8.png"]];
[_images addObject:[UIImage imageNamed:#"logo9.png"]];
[_images addObject:[UIImage imageNamed:#"logo10.png"]];
[_images addObject:[UIImage imageNamed:#"logo11.png"]];
[_images addObject:[UIImage imageNamed:#"logo12.png"]];
[_images addObject:[UIImage imageNamed:#"logo13.png"]];
[_images addObject:[UIImage imageNamed:#"logo14.png"]];
[_images addObject:[UIImage imageNamed:#"logo15.png"]];
[_images addObject:[UIImage imageNamed:#"logo16.png"]];
NSLog(#"ha ha ha:%d",_images.count);
UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row=0;
int coloumn=0;
for (int i=0; i< _images.count; i++)
{
UIImage *thumb=[_images objectAtIndex:i];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(coloumn*100+24, row*80+30, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
[view addSubview:button];
if (coloumn==2) {
coloumn==0;
row++;
}else {
coloumn++;
}
}
[view setContentSize:CGSizeMake(320, (row+1)*80+10)];
self.view=view;
[view release];
The problem with your code is here:
if (coloumn==2) {
coloumn==0; //-- ERROR!
row++;
this should be:
coloumn=0; //-- correct!
using == instead of =
Use the three20 library
iPhone SDK: Creating a Photo Gallery With Three20
Edited:
Your code are excellent except on wrong statement :
coloumn==0;
that's the reason your colomn always point to the 2nd coloumn.
So change it to coloumn = 0;
This works very well but you would have to make a few adjustments to the code when you change the size. Copy and paste it in your Viewdidload; make sure you have a UIScrollview with IB already setup with a link name scrollView.
int row = 0;
int colomn = 0;
int width = 79;
int height = 79;
int offset = 1;
// int diff = width * 0.5;
CGRect rect;
for (int i = 0; i < 45; i++) {
if(i == 0){
colomn = 0;
}else{
colomn += width + offset;
};
if(colomn >= ((int)scrollView.frame.size.width) - width){
colomn = 0;
row += height + offset;
}
rect = CGRectMake(colomn, row, width, height);
UIView *view = [[UIView alloc] initWithFrame:rect];
[view setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:view];
}
[scrollView setContentSize:CGSizeMake(320, row+height)];

Reloading table view in xcode

I have a text box in my app with a button and a table view.Inside this table view i am creating image views which will display some images.Now when i enter a number say 5 in the text box and then click the button then 5 images are displayed on the table view out of which 4 images are in the first row .Now the problem comes if i enter number less than 5 say 3 then i have to get only 3 images in the table view but still i am getting 5 images and if i enter more than 5 then i am getting the desired number on the table view.My piece of code is:-
if([imageArray count] >0)
{
[imageArray removeAllObjects];
}
int j = [text.text intValue];
for(int i=0;i<j;i++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
}
[tableView reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
if([imageArray count]>0)
{
imageView1.image=[UIImage imageNamed:#"CARRY.png"];
imageView1.image = [imageArray objectAtIndex:0];
[cell.contentView addSubview:imageView1];
[imageView1 release];
}
if([imageArray count]>1)
{
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView2.image = [imageArray objectAtIndex:1];
[imageView2 addSubview:button6];
[cell.contentView addSubview:imageView2];
[imageView2 release];
}
if([imageArray count]>2)
{
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(310, 5, 100, 125)];
imageView3.image = [imageArray objectAtIndex:2];
[cell.contentView addSubview:imageView3];
[imageView3 release];
}
if([imageArray count]>3)
{
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,125)];
imageView4.image = [imageArray objectAtIndex:3];
[cell.contentView addSubview:imageView4];
[imageView4 release];
}
}
else if(indexPath.row == 1)
{
if([imageArray count]>4)
{
UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
imageView5.image = [imageArray objectAtIndex:4];
[cell.contentView addSubview:imageView5];
[imageView5 release];
}
if([imageArray count]>5)
{
UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView6.image = [imageArray objectAtIndex:5];
[cell.contentView addSubview:imageView6];
[imageView6 release];
}
if([imageArray count]>6)
{
UIImageView *imageView7= [[UIImageView alloc] initWithFrame:CGRectMake(310, 5, 100, 125)];
imageView7.image = [imageArray objectAtIndex:6];
[cell.contentView addSubview:imageView7];
[imageView7 release];
}
if([imageArray count]>7)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,125)];
imageView8.image = [imageArray objectAtIndex:7];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
}
else if(indexPath.row == 2)
{
if([imageArray count]>8)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
imageView8.image = [imageArray objectAtIndex:8];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
if([imageArray count]>9)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView8.image = [imageArray objectAtIndex:9];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
}
return cell;
}
I am clearing the array and again adding images to it then reloading the table view but why is the output then not coming according to the needs.Please help.
Thanks,
Christy
The problem is that you're adding the image views but you aren't removing the image views that you've added earlier in case of cell reuse.