I'm playing with the tabbar, navigationBar and SegmentedBar in my application.
I have to show an picture from a HTTP like this :
- (void)viewDidLoad {
[super viewDidLoad];
// build the URL, perform the request and show the picture
NSURL *url = [NSURL URLWithString: #"http://api.clementhallet.be/15avril.png"];
//UIImage
[[UIImageView alloc] initWithImage:image];
image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
image.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}
It's running but the picture isn't where I want [exactly][1].
Thanks to help me!
This should be the right order:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
You've got to set the frame of your imageview for positioning. If not in interface builder done, do it like this:
image.frame = CGRectMake(x, y, width, height);
Related
I am trying to combine two image views and put them into another image view which I am assigning to background image of a button.Somehow this is not working.Could someone please check and let me know what exactly is wrong here.
UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"homesmall.png"]];
UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"update.png"]];
[image1 setFrame:CGRectMake(16,0,16,16)];
[image2 setFrame:CGRectMake(0,0,16,16)];
UIImageView *cellAcc = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,16)];
[cellAcc addSubview:image1];
[cellAcc addSubview:image2];
UIImage *image = [cellAcc image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
You are adding them as subviews. There is no additive property here. You will have to use quartz to draw them into an image context and extract the combined image.
Edit
You can replace
UIImage *image = [cellAcc image];
with
UIGraphicsBeginImageContext(cellAcc.bounds.size);
[cellAcc.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I have written a code for setting imageview ...as per the button clicked...but my problem is when i press the button it is giving me the error like...too many arguments to function ImageNamed...while executing this line...
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
the whole code is....
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView]; }
use UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort de nero%d.jpg",x]];
cheers :)
Try this
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
NSString *imageName = [NSString stringWithFormat:#"robort %d.jpg",x];
UIImage* photo = [UIImage imageNamed:imageName];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView];
}
You need to use:
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
Use this
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 40, 40);
self.navigationItem.titleView = imageView;
[imageView release];
Thanks for your help,
BR, doonot
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(70, 0, 40, 40);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
tmpTitleLabel.text = #"Mannschaft";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
[imageView release];
[tmpTitleLabel release];
Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.
How i can set the frame size of UIImage.
You can set the frame of the UIImageView:
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.image = [UIImage imageNamed:#"image.png"];
(and don't forget to [imgView release]; somewhere)
UIImage has no frame. It has only width and height. If you want to put it on your view you should create UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,12,123,123)];
imageView.image = image; // image - your UIImage
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
[self.navigationController.view addSubview:imageView];
I have added an image to my mainview using above code, how can I remove it from view so that it will not visible in next view?
I'm not sure that I understand the reason for doing this in the first place.
But you might set a tag value for this image view and then find it by that tag:
..
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
imageView.tag = 100001;
[self.navigationController.view addSubview:imageView];
// Why don't you release the image view?
[imageView release];
..
UIView *imageView = [self.navigationController.view viewWithTag:100001];
[imageView removeFromSuperView];
Not sure that all the code is correct - wrote it without XCode...