How to create a dynamic UIImageView - objective-c

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
}

Related

Add logo to UINavigationBar

I'm adding the following code to my app delegate to get my logo to the UINavigationBar, but it is not working. I have the Logo.png and Logo#2x.png inside the project.
What am I doing wrong?
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]];
[[UINavigationBar appearance] setTitleView:titleView];
Yes, iOS is case-sensitive. Try it again but change:
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]];
to
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
If I understand your question correctly, then what you'll want to do is to set the tileImage in your ViewController's navigation item like so:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up the nav bar
UIImage *titleImage = [UIImage imageNamed:#"Logo.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:titleImage];
}

Define background Image with UIImage

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;

images from an array

i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...

How to position a UIActivityIndicatorView in a UITableViewCell

I've this code for adding UIActivityIndicatorView to a UITableViewCell.
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(CGSizeMake(220, 150));
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
Currently the image is shown in the top left of the 220x150 box. Instead I would like to put it in center. How can I do that? I've tried to change the two first parameters on CGRectMake, but that didn't change anything.
You've added resizedSpacer as the imageView without actually doing anything to spinner's frame. You want something like:
CGSize spinSize = spinner.frame.size;
spinner.frame = CGRectMake((220-spinSize.width)/2., (150-spinSize.height)/2.,spinSize.width, spinSize.height);
[cell.imageView addSubview:spinner];
I don't think that the logic involving spacer is required. In fact, you may not even want to host the UIActivityIndicatorView in the UIImageView at all; why not position it directly within the tableview cell?

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..