Define background Image with UIImage - objective-c

I'm using a header file to set the background for my application. I have something like:
#define backgroundImage [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.jpeg"]]
but I want use UIImageView instead of UIColor. I know I can do:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)
[imageView setImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundView = imageView;
but how do I use it with #define?

#define is a preprocessor directive. What this is going to do is anywhere you use backgroundImage you will get [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.jpeg"]]
The best way to handle this is use the #define to specify the image name:
#define kBackgroundImage #"background.png"
And then use that in your code:
// Use the table view bounds so the background view is the size of the table view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds;
[imageView setImage:[UIImage imageNamed:kBackgroundImage]];
self.tableView.backgroundView = imageView;
If you want to however, you can do:
#define kBackgroundImage [UIImage imageNamed:#"background.png"]
And:
// Use the table view bounds so the background view is the size of the table view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds;
[imageView setImage:kBackgroundImage];
self.tableView.backgroundView = imageView;
If you choose to make the whole code block a preprocessor define, you can use \ to make new lines.
#define UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds; \
[imageView setImage:[UIImage imageNamed:kBackgroundImage]]; \
self.tableView.backgroundView = imageView;

Related

Change Text Lable to Image in XCode

I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];

UItableview headerview with custom uiview?

I am creating a custom uiview that contains an uilabel and an uiimage assigned to an uiimageview. I assign the custom uiview to the uitableview headerview but i only see the uilabel not the uiimage...please help this is my code...
UIView *containerView1 =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
UILabel *headerLabel1 =[[UILabel alloc]initWithFrame:CGRectMake(75, 4, 300, 20)];
headerLabel1.userInteractionEnabled = YES;
headerLabel1.text = [NSString stringWithFormat:#"- Latest update %# -", now];
headerLabel1.textColor = [UIColor brownColor];
headerLabel1.shadowOffset = CGSizeMake(0, 1);
headerLabel1.font = [UIFont boldSystemFontOfSize:13];
headerLabel1.backgroundColor = [UIColor clearColor];
UIImage *image = [UIImage imageNamed:#"line_seperator.png"];
UIImageView *imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, 30, 30)];
imageView.image = image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[containerView1 addSubview:headerLabel1];
[containerView1 addSubview:imageView];
[containerView1 addSubview:indicator2];
walltable.tableHeaderView = containerView1;
Any help appreciated.
Forgot to say that this code executes after the uitableview loads.
I have found it. The image was not right. The image was downloaded from the web as .gif . I opened it in photoshop and saved it as .png but it did not saved correctly. I created a new image in photoshop and copy the one i had in that new image. I saved it as .png and it worked.

Showing a Title and an Image Into the Table Header

I am trying to show into the header of my UITableViewController a title and under it an image illustrating the title. The next code (written into the viewDidLoad method) shows only the image, and this image over the rest of the table sections. How I can fix it to do what I want?
// Creates a header view.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
headerLabel.text = self.name;
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.backgroundColor = [UIColor clearColor];
GRect imageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *headerImage = [[UIImageView alloc] initWithFrame:imageRect];
UIImage *image = [UIImage imageNamed:self.imagePath];
[headerImage setImage:image];
headerImage.opaque = YES;
[containerView addSubview:headerLabel];
[containerView addSubview:headerImage];
self.tableView.tableHeaderView = containerView;
Thanks for reading.
Just adjust the views' frames accordingly. The container view's frame must be tall enough to contain the label and image view, and the image view's y coordinate must be set so that it is positioned below the label. The frame rects you are using in your code do not match.

Cocoa Touch - Adding a UIImageView programmatically?

How can I create and position a new imageview in objective-c?
Thanks!
I tried this but it doesnt seem to do anything...
-(void)drawStars{ //uses random numbers to display a star on screen
//create random position
int xCoordinate = arc4random() % 10;
int yCoordinate = arc4random() % 10;
UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView
starImgView.image = [UIImage imageNamed:#"star.png"];
[starImgView release];
I placed this method in my viewcontroller. I see some CG stuff do I need to import core graphics or something? (what is core graphics anyway?)
You are asking about iPhone image view. Right? Then try this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.
Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.
imgView.image = [UIImage imageNamed:#"a_image.png"];
And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use
imgView.contentMode = UIViewContentModeCenter to place the desired image to the center of the view. The available contentModes are listed here
You haven't added your view as a subview to another view, meaning it isn't in the view hierarchy.
Assuming you are doing this in a view controller, it might look something like:
[self.view addSubview: imgView];
(void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}
I had problem with passing the value NSSArray to NSString,
I got the answer:
//Display all images....
NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
cell.myimages.image=[UIImage imageNamed:imgstring]; //displaying Images in UIImageView..noproblem.
// Display all numbers...
cell.mylables.text=[NSString stringWithFormat:#"%#", [mysetobjectAtIndex:indexPath.row ]]; //showing results fine ArghyA..

How to create a dynamic UIImageView

I want to create an UIImageView with, for example, "Trend.jgp" for a special case (if-condition). I tried various ways with CGRectMake but I am not able to create this picture dynamically to a special place. Furthermore, I can't use the function setimage like it's described in the Developer Documentation.
Has anyone got an idea how to realise my plan?
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Trend"]];
imageView.frame = CGRectMake(....) or imageView.center = CGPointMake(...)
Add this view to your container view, i.e. [myView addSubview:imageView];
UIImageView *imageView = nil;
if (specialCase) {
imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"Trend.jpg"]];
} else {
//whatever you want to do in the other case
}