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]];
Related
I am new to iPhone,
I want to do scrolling in my image which is inside scrollView but i am unable to scroll my image,
Here is my code snippet,
UIImage *image1 = [UIImage imageWithContentsOfFile:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
I have taken ScrollView in my .xib file.
Any help will be appreciated.
Try this code:
UIImage *image1 = [UIImage imageNamed:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
We use initWithContentsOfFile when we know the path of the image.Using it with imageName will give a null image.
try following instead
self.scrollView.contentSize = self.imageView.frame.size;
I am new to programming and developing my first application, but I want to know that how can I display image to imageView.
Programmatically :
From http://www.iphoneexamples.com/:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
Where myImage.png is an png format image which you will import to your resources folder.
Using Nib(Xib File):
Open your.xib file.
Go to Tools -> Library.
Drag and drop one UIImageView on your .xib.
Open Attribute Inspector of the ImageView(Press Cmd + 1).
You'll get one Image property. Set the image you want from your Resource folder using the drop-down list.
There you got it. :-)
// First way
myImage.image = [UIImage imageNamed:#"myImage.png"];
// Second way
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/myImage.png"];
myImage.image = [UIImage imageWithContentsOfFile:fullpath];
Check out UIImageView class reference here
Read http://www.iphoneexamples.com/
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
UIImageView *imageView=[[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:#"yourImage.png"]];
(If you have created imageView from IB and have outlet-ed it then ignore First Line)
Try it using:
imgView.image = [UIImage imageNamed:#"test.png"];
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'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);
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