I'm doing this to load an image into a tableview cell:
UIImage *image = [UIImage imageNamed:#"myimage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake( 0, 0, cell.frame.size.width, cell.frame.size.height );
imageView.contentMode = UIViewContentModeScaleToFill;
[cell addSubview:imageView];
[imageView release];
return cell;
However, no image displays as the cell's background. This is in a new navigation app. The above looks just like the steps in Interface Builder when you are using a custom cell. The image is in the app bundle. What am I doing wrong?
Are you trying to set the cell's background, or just add an image? If you're trying to set its background, you should use the cell.background property.
imageView.frame = cell.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.backgroundView = imageView;
Related
I'm wondering how to set an imageview to the center of a tableview cell dynamically.
Here is my current code.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];
imgView.image = [UIImage imageNamed:#"friendsCelluserpic.png"];
cell.imageView.image = imgView.image;
Try this code :
This will align your image horizontally centre in TableViewCell
float imgwidth=40;
float imgheight=40;
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];
[cell.contentView addSubview:imgView];
try this, :)
UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);
[cell.contentView addSubview:imgView];
return cell;
you can code like this [cell.contentView addSubview:imgView];
I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.
How i can set the frame size of UIImage.
You can set the frame of the UIImageView:
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.image = [UIImage imageNamed:#"image.png"];
(and don't forget to [imgView release]; somewhere)
UIImage has no frame. It has only width and height. If you want to put it on your view you should create UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,12,123,123)];
imageView.image = image; // image - your UIImage
i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
[self.navigationController.view addSubview:imageView];
I have added an image to my mainview using above code, how can I remove it from view so that it will not visible in next view?
I'm not sure that I understand the reason for doing this in the first place.
But you might set a tag value for this image view and then find it by that tag:
..
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
imageView.tag = 100001;
[self.navigationController.view addSubview:imageView];
// Why don't you release the image view?
[imageView release];
..
UIView *imageView = [self.navigationController.view viewWithTag:100001];
[imageView removeFromSuperView];
Not sure that all the code is correct - wrote it without XCode...