UIImage in UITableViewCell - objective-c

I'm trying to put image in UITableViewCell. I use SSMessages style for my messages.
Tried something like cell.imageView.image = [UIImage imageNamed:message.messageText]; but it isn't work. Any suggestions? Maybe need my other code?

Use SSMessageTableViewCellBubbleView's leftBackgroundImage and add it this way to your table view
SSMessageTableViewCellBubbleView *imageBubbleView = [SSMessageTableViewCellBubbleView alloc] init];
imageBubbleView. leftBackgroundImage = [UIImage imageNamed:message.messageText];
[cell.contentView addSubview:imageBubbleView];
I expect this to work!

First set the background color of the image view for troubleshooting:
cell.imageView.backgroundColor = [UIColor colorWithRed:(255.0f/255.0f) green:(0.0f/255.0f) blue:(0.0f/255.0f) alpha:1.0f];
Set the image:
cell.imageView.image = ...;
Set the image view frame:
cell.imageView.frame = CGRectMake(x, y, cell.imageView.image.size.width, cell.imageView.image.size.height);
Add the image view as a subview of the cell.
Bring this subview to the front:
[cell bringSubviewToFront:[cell.imageView superview]];

You may need to call [cell setNeedsLayout] or [cell setNeedsDisplay] in order to get the cell to update the frame of the UIImageView for the new image. Simply setting image on the UIImageView will do nothing if the frame of the UIImageView is CGRectZero.
To see if the frame is zero, try adding a background color or a layer border using:
cell.imageView.layer.borderWidth = 1.f;
cell.imageView.layer.borderColor = [UIColor redColor].CGColor;

Related

UIView changing the size of the image?

I am trying to add UIImage to UIView .
The image is exactly in the size of the view -as i defined the view rect .
Somehow i see that the images is displayed wider,and taller(its stretched ) .
Why does my view is change the image size ?
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
EDIT :
I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?
Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)
imageView.contentMode = UIViewContentModeScaleAspectFit;
use
imageView.layer.masksToBounds = YES;
which will restrict image to be wide and taller
If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).
of course your image is going to be stretched and can not be shown in your view.
Because its frame is a lot bigger than the size of the UIView.
you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView = [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
and of course if you can make the size of the uiimageview smaller ;)

roundedCorner on UIImageView applies incorrectly to UITableCell

Hi there I have a table cell which contains amongst other things a UIImageView. I want to make this UIImageView have a rounded corner. Here's the code that I have (relevant snippets):
UIImageView* image;
image = (UIImageView*)[cell viewWithTag:0]; // get the UIImageView from the nib
image.image = [UIImage imageWithData:imageData]; // imagedata contains the image
image.layer.cornerRadius = 10.0;
image.layer.masksToBounds = YES;
image.clipsToBounds = YES;
When I do this, the entire UITableViewCell gets the rounded corners, instead of the UIImageView (see attached image).
Can someone tell me why the rounded corners are applied to the table cell rather than the uiImageView?
http://i.stack.imgur.com/CnkyQ.png
(UIImageView*)[cell viewWithTag:0];
In cell subviews you have only UITableViewCellContentView with tag == 0.
You can manually add the UIImageView to cell.
The problem should be the ...Tag:0:
image = (UIImageView*)[cell viewWithTag:0]; // get the UIImageView from the nib
If you are using a non-custom UITableViewCell replace that line with:
image = (UIImageView*)[cell imageView];
Else, replace the tag number.

ios tableviewcell several objects one a cell programatically , setting prototype cell programatically , custom table view cell style,

hello I built this tableview using the InterfaceBuilder and storyboard I used custom for the style of the cell and content prototype of the table view, this is the image:
custom tableview
but now I need to redo everything programmatically I tried insert an imageview in the cell using this code
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(300, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
cell.imageView.image = imgView.image;`
but the image stays static unresponsive to CGRectMake thanks for you help
Use this code to the Cell:
// Make your frame origin 0,0 as below done.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
[cell addSubview:imgView];
[imgView release];
imgView = nil;
Add
imgView.clipsToBounds = YES;
To limit display of the image to the frame of the image view.
Also, you are adding the image of the image view to the cell's image view, which is a different object, without using your image view and its frame. You actually do not need an image view if you are just using the image. What you want is
cell.imageView = imgView;
or
[cell.contentView addSubview:imgView];
The latter will be preferable if you want to place it exactly and add more image views as in your screen shot.

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.
I use the same code to create a custom view that I assign to backgroundView, and that works fine.
What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?
- (UIView*) makeSelectedBackgroundView
{
// dimensions only for relative layout
CGRect containerFrame = CGRectMake(0, 0, 320, 40);
UIView* containerView = [[UIView alloc] initWithFrame:containerFrame];
containerView.autoresizesSubviews = YES;
// dimensions only for relative layout
CGRect subframe = CGRectMake(5, 5, 310, 30);
UIView* subview = [[UIView alloc] initWithFrame:subframe];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview.backgroundColor = [UIColor redColor];
subview.layer.cornerRadius = 5;
subview.layer.borderWidth = 2;
subview.layer.borderColor = [UIColor greenColor].CGColor;
[containerView addSubview:subview];
return containerView;
}
As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)
This code might be helpful for you:
UIImageView *cellImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,
0,
cell.frame.size.width,
cell.frame.size.height
)];
cellImageView.contentMode = UIViewContentModeScaleAspectFit;
// normal background view
[cellImageView setImage:[UIImage imageNamed:#"*<ImageName>*"]];
[cell addSubview:cellImageView];
[cell sendSubviewToBack:cellImageView];
[cellImageView release], cellImageView = nil;
Here cell is an object of custom UITableViewCell.
Also you can set backgroundColor property.
I would try to set the alpha for both containerView and subView to 1.0
[containerView setAlpha:1.0];
...
[subview setAlpha:1.0];
this should make your controls totally opaque.
You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.
Try setOpaque:YES on your views.
In the end, I ended up subclassing UITableViewCell which contained a custom view object, and that worked.

Image at the top of the tableview

Hey I am making an app that has to show a big image at the top of the grouped tableview, I don't want the image to be in tablecell because it looks stupid that way. I just want the image to float there, but I have no idea how to do it? Thanks!
Use the table view's tableHeaderView property, like this:
UIImageView *topImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my logo.png"];
topImage.backgroundColor = [UIColor clearColor];
topImage.frame = CGRectMake(0, 0, theTableView.bounds.size.width, 100); // replace 100 with the height of your image, plus a bit if you want it to have a margin
topImage.contentMode = UIViewContentModeCenter;
theTableView.tableHeaderView = topImage;
[topImage release];