Stop TableView cell UIImage resizing in iOS - ios

I have a standard (i.e. not a custom) TableView cell with text and images, the problem is my images are different sizes and I want to put them in as something like UIViewContentModeScaleAspectFill. I simply want the UIImage to be 54x54 and not resizes all the time - I just want them square!
There are lots of ideas out there but none of them work, the closest I came worked BUT the image was on top of the text (it added another image as a subview).
I am not keen on using a custom cell as I use edit mode to drag stuff about and I have not figured that out yet with custom cells.
EDIT
I latest I have tried;
UIImage *tempImage = [UIImage imageWithData:data];
CGSize size = CGSizeMake(54, 54);
UIGraphicsBeginImageContext(size);
[tempImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = scaledImage;
I have this in the cellForRow which I hoped would do it;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
This only works on custom cells;
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,54,54);
}
This works well but it covers the text;
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[cell.contentView addSubview:imgView];
This shows the exact problem I am having;
Stackoverflow problem
ANOTHER EDIT
okay this worked, thanks;
[cell setIndentationWidth:54];
[cell setIndentationLevel:1];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:myImages[indexPath.row]];
[cell.contentView addSubview:imgView];

I recommend you to subclass UITableViewCell and create your own custom cell :D
but, anyway... try by adding this
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *imgView = nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 54, 54)];
imgView.backgroundColor=[UIColor clearColor];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.tag = 123;
[cell.contentView insertSubview:imgView atIndex:0];
[cell setIndentationWidth:54];
[cell setIndentationLevel:1];
}
else
{
imgView = (UIImageView*)[cell.contentView viewWithTag:123];
}
imgView.image = [UIImage imageNamed:#"myImage"];

Related

How to position a image in customized section header so that it appears correctly in both the orientations?

I have a customized section header, in viewForHeaderInSection, I have a view and a button on that so it will be like clicking the section header some action will be performed.Now I need an image in the section header so that it changes on clicking the section header.So I added a image view to the view in viewForHeaderInSection but the image position is not correct,it is different in landscape and portrait.I need the image in the right corner of the section header.I need to do it immediately.
Please help.
Thanks
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
oky try this hope this helps u
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame: CGRectMake(view.bounds.size.width - 20, 1.5f, 8.0f, 18.0f); //set the offset from right
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
and in rotation handling method u need to reload or update the UI
for example
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//so u dont want to reload the tableview then u can reload the sections
// [self.tableVIew reloadData]; //just try this (comment)
//try this
NSArray *cells = [self.tableVIew visibleCells];
NSIndexPath *indexPath = [self.tableVIew indexPathForCell:(UITableViewCell *)[cells lastObject]];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, totalSectionCount)]; //try by placing the all the section count
[self.tableVIew reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
You should have something like this:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
[imgView setFrame:CGRectMake(parentView.frame.size.width-50, 5, 30, 30)];
[parentView addSubview:imgView];
The sizes in CGRectMake are only as an example, but make sure your UIImageView is always related to you parentView.frame.size.width... that should do it!
EDIT: After your code, you should replace:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
with
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width-15, 1.5f, 8.0f, 18.0f)];
play with the -15 to put the image where you wish...
i hope it helps!

I want to know which image is pressed in UIScrollView in objective c

I have UIScrollView that consists of UIImageViews. When I touch any imageView, I want to display this UIImageView's image in other window. how can I do that? any ideas .
I use UITapGestureRecognizer but I cant find which imageView is pressed.
this is my code:
for (NSArray *photo in self.photos) {
imageName = [photo valueForKey:#"filename"];
imageName = [imageName stringByAppendingString:#".jpg"];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView;
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
imageX += imageSize + 10;
if (imageX + imageSize >= self.view.bounds.size.width){
imageX = 15;
imageY += imageSize + 10;
}
imageView.userInteractionEnabled = YES;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goFullPhotoGallery:)];
[imageView addGestureRecognizer:tap];
[scrollView addSubview:imageView];
}
for this type of task i generally use a UIButton over image view and addTarget to them and get image with button tag.
A better solution would be to use a UITableView or a UICollectionView, and listen to cell selection events...
Try this -
for (NSArray *photo in self.photos) {
imageName = [photo valueForKey:#"filename"];
imageName = [imageName stringByAppendingString:#".jpg"];
UIImage *image = [UIImage imageNamed:imageName];
UIButton *starBtn = [[UIButton alloc]initWithFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
[starBtn setBackgroundColor:[UIColor clearColor]];
[starBtn setImage:image forState:UIControlStateNormal];
[starBtn setTag:i+1];
[starBtn setTitle:imageName forState:UIControlStateNormal];
[starBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[starBtn addTarget:self action:#selector(goFullPhotoGallery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:starBtn];
}
and goFullPhotoGallery function like this
-(void)goFullPhotoGallery:(UIButton *)imgView{
NSString *imgName = imgView.titleLabel.text;
}

merging uilabel and uiimageview

I picked an image from the photo album and put it in an UIImageView called imageView
I then added some text over the image using UITextView as source of input and a UILabel on top of the uiimageview to display the text.
So far so good.
Now I'm trying to merge the label and the imageview in order to do other stuff with that but I 'm not getting it to work. I have tried some solutions given to similar questions but they didn't work for me.
I tried this
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_displaylabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
it tells me that Received type "CAlayer for instance message is a forward declaration".
any other suggestions?
Try this:
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Make sure that the UILabel is a subview of your UIImageView, so that rendering the layer will include all sublayers (the UILabel included). It seems like you forgot to include UIGraphicsEndImageContext(); too, which may have been the issue
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
[tempView addSubview:imgView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempView.frame.size.width/2, 0, 200, tempView.frame.size.height/2)];
[label setText:#"Hello... You there"];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[tempView addSubview:label];
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Header image not resizable in expandable table

I am pretty new to Xcode and this simple problem has been driving me mad! I have created an expandable table that works fine. This is some of the code on a UIView subclass for the section that expands when you tap on a cell:
- (id)initWithFrame:(CGRect)frame WithTitle: (NSString *) title Section:(NSInteger)sectionNumber delegate: (id <SectionView>) Delegate
{
self = [super initWithFrame:frame];
if (self) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(discButtonPressed:)];
[self addGestureRecognizer:tapGesture];
self.userInteractionEnabled = YES;
self.section = sectionNumber;
self.delegate = Delegate;
CGRect LabelFrame = CGRectMake(100, 100, 100, 100);
LabelFrame.size.width -= 100;
CGRectInset(LabelFrame, 1.0, 1.0);
//BUTTON
CGRect buttonFrame = CGRectMake(LabelFrame.size.width, 0, 100, LabelFrame.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
//[button setImage:[UIImage imageNamed:#"carat.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:#"carat-open.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(discButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.discButton = button;
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
//HEADER LABEL
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(22, 12, sectionHeaderView.frame.size.width, 35.0)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.text = title;
label.font = [UIFont fontWithName:#"AvenirNext-Bold" size:20.0];
sectionHeaderView.backgroundColor = [UIColor clearColor];
//label.textAlignment = UITextAlignmentLeft;
[self addSubview:label];
self.sectionTitle = label;
}
return self;
}
I have a custom image on the cell #"gradient1.png" but I don't seem to be able to resize it? Here is the header code in the UITableViewController:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
if (!array.sectionView)
{
NSString *title = array.category.name;
array.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 10, self.tableView.bounds.size.width, 0) WithTitle:title Section:section delegate:self];
}
return array.sectionView;
}
Sorry if this is a trivial question, your help is greatly appreciated!
I don't see where you are trying to resize the image so I can't offer any help in why it is not resizing, but I found this confusing:
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
In this part of code you create two imageviews, then you resize one and add the other to the cell (I'm assuming cell) subviews. Is it possible that you are trying to resize the view that you never added as a subview of the cell?
Also, resizing should happen inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
Make sure that you are resizing the proper view, ten make sure you are resizing it in the proper place.

iOS how to add UIView to tableview cell effectively

I have a table going where each cell has a UIView in it that is fed from an XMLParser feed. If I leave it how it is, each time I refresh the views build upon each other because they are not getting released/removed, which makes total sense. How would I go about writing my code to avoid that problem, and not having to rebuild the view's each time I refresh. I am stuck in the logic of it and also the actual syntax. Thanks.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture.png"]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
[selectionView setBackgroundColor: [UIColor clearColor]];
cell.selectedBackgroundView = selectionView;
}
// Display content in each cell
cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 300, 65)];
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
cellSeparator.layer.borderWidth = 1.0;
cellSeparator.layer.borderColor = [UIColor blackColor].CGColor;
[cell addSubview:cellSeparator];
callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
callTypeLabel.text = [currentCall currentCallType];
callTypeLabel.backgroundColor = [UIColor clearColor];
callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
[cellSeparator addSubview:callTypeLabel];
locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
locationLabel.text = [currentCall location];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:10.0];
[cellSeparator addSubview:locationLabel];
unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
unitsLabel.text = [currentCall units];
unitsLabel.backgroundColor = [UIColor clearColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10.0];
[cellSeparator addSubview:unitsLabel];
stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
NSString *station = [#"Station: " stringByAppendingString:currentCall.station];
stationLabel.text = station;
stationLabel.backgroundColor = [UIColor clearColor];
stationLabel.font = [UIFont boldSystemFontOfSize:11.0];
[cellSeparator addSubview:stationLabel];
if ([currentCall.county isEqualToString:#"W"]) {
UIImage *countyImage = [UIImage imageNamed:#"blue.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
} else {
UIImage *countyImage = [UIImage imageNamed:#"green.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
}
if ([currentCall.callType isEqualToString:#"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:#"red.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
} else {
UIImage *callTypeImage = [UIImage imageNamed:#"yellow.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
}
return cell;
}
If I understand your question correctly, I think the solution you want is to subclass UITableViewCell, set up your subviews (UIImageViews and all that) and make them properties on your subclass. Then you can
if (!cell.imageView) {
cell.imageView = [[UIImageView alloc] initWithFrame:myFrame];
}
then just set the image. That way you're sure to not stack image views on top of one another and the correct image will get set when the cell is dequeued.
EDIT:
Make a class called MyTableViewCell (or whatever you want to call it but make sure it's a subclass of UITableViewCell)
Add #property (strong, nonatomic) UIImageView *imageView;
Then use MyTableViewCell instead of UITableViewCell in you cellForRowAtIndexPath and you can access the image property of your custom table view cell, check if it exists, if not create it (which is what the above code does) and then set the image. I would give you code but I'm on my phone. Google subclassing UITableviewCell. You should find a ton of examples.

Resources