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.
Related
i have one image as 640*960,and my image view has 300*300.i set the content mode the image view as aspect fit, now i load the image to the imageView it looks follow..
after loaded the image to the image view i need the width and height of the image from the imageView.
Did you try?
#import AVFoundation;
UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageNamed:#"paint5.png"];
CGRect yourFrame = AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);
NSLog(#"%#",NSStringFromCGRect(yourFrame));
I think thats what u are looking for.
AV Foundation Functions Reference
You can get height and width of image in following way:
UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageNamed:#"paint5.png"];
int imgHeight = image.layer.frame.size.height;
int imgWidth = image.layer.frame.size.width;
NSLog(#"height of image %i",imgHeight);
NSLog(#"widht of image %i",imgWidth);
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 ;)
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.
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;
I want to have a pointer to an UIImage view so that I don't have to repeat a lot of my code. I need to choose the image based on which button it is but then at the end I apply the same code to whatever digit. oneDigit, twoDigit, and threeDigit are all #property(nonatomic, retain) in the header file. I want imageView to be able to point to one of these UIImageViews. Right now the UIImageViews are not being removed with removeFromSuperview because imageView is not pointing to the objects but creating its own object..
Currently when a button is pressed a Image is added. I want the previous image removed and the new image drawn according to what button was pressed. The code works how I want if I replace imageView with oneDigit, but that would require me to add more code for each variable.
See simplified example below:
- (void)viewDidLoad
{ ...
UIImageView *oneDigit;
UIImageView *twoDigit;
UIImageView *threeDigit;
}
-(IBAction)pressButton:(id)sender {
UIImage *image;
UIImageView *imageView;
UIButton *btn = (UIButton*)sender;
int value = [btn.titleLabel.text intValue];
switch (value) {
case 10:
case 15:
case 20:
[twoDigit removeFromSuperview]; //Remove old image from screen
imageView = twoDigit; //Update imageView pointer for current case
image = [UIImage imageNamed:#"twoDigit.png"];
break;
case 1:
case 2:
case 3:
case 4:
case 5:
[oneDigit removeFromSuperview];
imageView = oneDigit;
image = [UIImage imageNamed:#"oneDigit.png"];
break;
case 100:
case 200:
case 300:
[threeDigit removeFromSuperview];
imageView = threeDigit;
image = [UIImage imageNamed:#"threeDigit.png"];
break;
default:
break;
}
CGPoint point = btn.frame.origin;
CGFloat xposition = point.x - ((image.size.width - btn.frame.size.width) /2);
CGFloat yposition = point.y - ((image.size.height - btn.frame.size.height) /2);
imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(xposition, yposition, image.size.width, image.size.height) ];
imageView.image = image;
imageView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imageView];
}
Assuming that oneDigit, twoDigit and threeDigit are UIImageViews, you can just do something similar to
imageView = oneDigit;
Since both are pointers, setting imageView to oneDigit will simply point imageView to the location of oneDigit. When you set it to the new image, don't autorelease it first, because if it's set to threeDigit for example, it will autorelease threeDigit too.
Also, you don't need to set the image property of imageView manually after setting imageView to the new image, because it's essentially referencing the exact same UIImageView with the same image; it's just under a different name.