Add a white background to a .png UIImage file with HSImageSidebarView - iphone

How can I add a white background to a .png UIImage file with HSImageSidebarView?
I have tried creating a UIImageView then showing that on the sidebar, but unfortunately the app crashes with an NSException. Any ideas on how to add a white background to the image files on the sidebar if they are a UIImage instead of a UIImageView.

You will try this code add one scrollview on nib and set its contentsize on viewdidload
CGFloat x = 20;
for (int i = 0;i<[playerScores count];i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 10, 60, 60)];
view.backgroundColor=[UIColor blueColor];
NSString *text = [NSString stringWithFormat:[playerScores objectAtIndex:i]];
[view setText:text];
[yardsScrollView addSubview:view];
[myLabel release];
x = x+125;
}
PlayersScore is a NSMutable array which have many text like how much view You want.

#objc-obsessive
You will need to loop through the subviews to find whether it is of kind UIImage or UIImageView by this:
id object;
if([object isKindOfClass:[UIImage Class]]) {
//Do whatever u want to here by creating a new imageView on the top of this image
//Something like this:
UIImage *image =(UIImage*)object;
UIImageView *imgView=[[uiimageview alloc]initwithimage:image];
//Now perform the background view operations on imgView i.e. instance of UIImageView.
}
//Hope it works.

Related

Changing view elements on action

I have been pondering for a while how to change the items on a UIView when you hit a button. What I would like to do is have a UITextView with text on it, then hit a button and have a UIImage appear on the screen. What I don't know how to do is how to put them together on the same view. Any help would be greatly appreciated!
To clear the content of a UIView you need to do:
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
Than what you will need to do is just add the new UIImage to an UIImageView and than do:
[view addSubView:myImageView];
it should look like this:
UIImage *myImage = [UIImage imageNamed:#"image name"]; // This is the image (UIImage)
UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage]; // Insert the image to an UIImageView
[self.view addSubView:myImageView]; // Add the UIImageView to the view
running all of this code will:
clear the view
insert the image to the view
1.Place an IBoutlet uiimageview on textview
2.at viewdidload set its property hidden YES
3.On button click method set its hidden property to NO
UIImage *myImage = [UIImage imageNamed:#"image name"];
IBOutlet UIImageView *myImageView= [[UIImageView alloc] initWithImage:myImage];
connect this to imageview on uitextview where you want to fix that image
In button method add this line
myImageView.hidden = NO;

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

Three20 TTTableItem elements background colour changed to white in IOS5

I have a TTTableViewController with a background image in which I insert items of type TTTableLink. Running the app if I drag the cells out of the screen by scrolling up and down for example, when they enter the screen again their background colour is changed to white. I have noticed this problem only in iOS5.
I don't really get when the background is changed, any ideas of how to preserve clear background for the table cells
Here is my code:
-(void) loadView {
[super loadView];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundView.frame = CGRectMake(0, 0, 320, 416);
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];
}
-(void) createModel {
TTListDataSource* listDataSource= [[[TTListDataSource alloc] init] autorelease];
TTTableLink *item = [TTTableLink itemWithText:itemName URL:targetURL];
[listDataSource.items addObject:item];
}
There's a pending pull request on three20 which fixes this issue. see https://github.com/facebook/three20/pull/689. post a comment on it, so the repo owner will merge it. I think he forgot about this project :-)
or you can either fix it by changing that line in your source code

Put small images on the top of UIImageView?

I've got a UIImageView in a page that gets its image from the Interface builder and I'm trying to put small icons on the top of it (it's a small map and I want to put icons on it). I know it's probably very simple but I tried to look it up in the documentation but it pretty much got me more confused.
Using Interface Builder, can't you just drag a UIImageView into an existing UIImageView? In effect, you end up with one UIImageView embedded within another.
You should also be able to easily set the hidden property of the "small map" UIImageView in code, depending on if that UIImageView is needed or not.
Hope this helps. Good Luck.
Let It Be Known
you could compose your own UIView by adding both the large and small UIViewImage views.
I have illustrated my approach below with the Pseudocode .
-(id) initwithFrame:(CGRect) frame
{
if(self = [super initWithFrame:frame])
{
iContainer = [[UIView alloc] initWithFrame:frame];
[iContainer addSubViews:iLargerUIImageView];
[iContainer addSubViews:iSmallUIImageView];
[self.view addSubViews:iContainer];
}
return self;
}
-(void) layoutSubviews
{
CGRect myRect = self.frame;
iContainer.frame = myRect;
//Give the location to iLargerUIImageView as per your requirement.
iLargerUIImageView.frame = CGRectMake(...,...,...,...);
//Give the location to iSmallUIImageViewas per your requirement.
iSmallUIImageView.frame = CGRectMake(...,...,...,...);
}
-(void) dealloc
{
[iContainer release];
[iLargerUIImageView release];
[iSmallUIImageView release];
}
try this code:
UIImageView *backgroundImageView = (UIImageView *)[self viewWithTag:kBckGndImag];
if(!backgroundImageView){
UIImage *imageName = [UIImage imageNamed:kpointsChartBig];
backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, imageName.size.width, imageName.size.height)];
backgroundImageView.image=imageName;
[backgroundImageView setTag:kBckGndImag];
[pointImageView addSubview:backgroundImageView];
[backgroundImageView release];
}
UIImageView *foregroundImageView = (UIImageView *)[self viewWithTag:kForGndImage];
if(!foregroundImageView){
foregroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kpointsColoredChartBig]];
foregroundImageView.contentMode = UIViewContentModeLeft;
foregroundImageView.clipsToBounds = YES;
[pointImageView addSubview:foregroundImageView];
[foregroundImageView release];
}

UITableViewCell background image and selection problem

It's my first iphone app, and i have trouble with styling my tableView.
I have two images (png), one for standard cell state, and one for selected state.
In my subclassed cell, I tried the following :
1) setting up front the backgroundView and selectedBackgroundView
UIImage *ib = [UIImage imageNamed:#"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.backgroundView = back;
[back release];
UIImage *is = [UIImage imageNamed:#"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is];
self.selectedBackgroundView = selected;
[selected release];
The standard cell is fine, but when selected, the two images are shown.
2) just playing with background view on selection :
// storing the 2 uiviews in class attributes
UIImage *ib = [UIImage imageNamed:#"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.storedStandard = back;
[back release];
UIImage *is = [UIImage imageNamed:#"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is];
self.storedSelected = selected;
[selected release];
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.backgroundView = self.storedSelected;
}
else {
self.backgroundView = self.storedStandard;
}
}
It nearly works, but the images don't fit the cell, i don't know to stretch them to the size of the cell.
For me, the first solution should have been the one, based on the properties' names, and the second solution seems like a hack (like 90% of the tutorials i seen btw), so i'm a bit frustrated.
To sum up : why the first one doesn't work, and how could i force images to take all the cell space ?
Thanks a lot :)
if you have a UITableViewDelegate, you should be able to just change the backgroundView's image in the willSelectRowAtIndexPath: method and then change it back in the didSelectRowAtIndexPath: method. This is a bit of a hack however.