I'm new to iPhone dev.
Now i have a project and its directory structure looks like this :
Tutorial
\__Tutorial\
\__1.png
\__TutorialViewController.h
\__TutorialViewController.m
\__TutorialViewController.xib
\__Supporting Files\
\__Frameworks\
\__Products\
I tried to use [UIImage imageNamed:#"1.png"] to render an image to the view:
UIImage *image = [UIImage imageNamed:#"1.png"];
imageView.image = image;
[image release];
But the image doesn't shows up.
So i wonder where should i place the images ?
And additionally, if i got lots of images (like 1000 number), what should do ?
Here's my code :
#import "TutorialViewController.h"
#implementation TutorialViewController
#synthesize labelText;
#synthesize imageView;
-(void) click:(id)sender {
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:#"%#",titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:#"Blue"]){
NSLog(#"Blue");
imageView.image = [UIImage imageNamed:#"a.png"];
}else{
imageView.image = [UIImage imageNamed:#"b.png"];;
NSLog(#"Else");
}
labelText.text = newText;
[newText release];
}
You should create a folder named Resources and add images there.
UIImage *image = [UIImage imageNamed:#"IMG_0270.PNG"];
imgView_.image = image;
I had used the above code and it works fine on my system. May be you haven't connected the outlet for imgView in the interface builder.
EDIT:
The problem I found is you are not adding it as a subview. You should change your code like this :
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"]];
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:#"%#", titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:#"Blue"]) {
NSLog(#"Blue");
imageView.image = [UIImage imageNamed:#"IMG_0270.png"];
} else {
imageView.image = [UIImage imageNamed:#"b.png"];
NSLog(#"Else");
}
[imageView setFrame:CGRectMake(10, 10, 230, 123)];
[self.view addSubview:imageView];
// labelText.text = newText;
[newText release];
there is no prbm with ur code. but Some time image become corrupt , due to this reason it not show, try some other images , and if image is not added at bundle path add at
project target > build phase > copy bundle Resources
. might be it will help.
You have problem at this statement
[image release];
The easiest way to remember when to use release is NARC keyword :)
Always remember to make release call in 4 cases.
N New
A Alloc
R Retain
C Copy
Try to remove the first line of your method, if you've connected the UIImageView in the xib you don't need to re-alloc it. Moreover call self.imageView.image instead just imageView.image.
Related
I have used the animated images on image view concept. It is working fine.Now whenever when I touch that image view I want to check which image is placed on that image view. How should I do this. I have used the following code to make the images animated.
addimage.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Softindia.png"],
[UIImage imageNamed:#"images.png"],
nil];
addimage.animationDuration=25.0;
addimage.animationRepeatCount=0;
[addimage startAnimating];
[self.view addSubview:addimage];
Now what should I do on the touch event of image view. Please provide me some solution to resolve this.
Thanks in advance.
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imagetap)] autorelease];
[imgview addGestureRecognizer:tapGesture];
-(void)imagetap
{
// your code what ever you want on touch
}
I am affraid that there is no such property inside UIImage which help you to find image name.
you can use this tricky method for solving your problem.
create an property
#property(nonatomic, retain) NSMutableArray * imageArray;
and then synthesis in .m file
#synthesis imageArray
afterward perform following action
self.imageArray = [[NSMutableArray alloc] init];
UIImage *myImage1 = [UIImage imageNamed:#"Softindia.png"];
[imageArray addObject:myImage1];
//Add Second Image
UIImage *myImage2 = [UIImage imageNamed:#"images.png"];
[imageArray addObject:myImage2];
addimage.animationImages= imageArray;
and where ever you are getting your image check do following action
UIImage image = addimage.image;
index = [imageArray indexOfObject:image];
if(index == 0){
//Do action for softindia.png
}
else {
//Do action for images.png;
}
In .h file I declare this;
IBOutlet UIImageView *image;
In .m file;
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
image = [[UIImageView alloc] initWithImage:image1];
image.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
and I connected the UIImageView from the Interface builder. But I need to do this Only by code (without using the Interface Builder). Can someone help me modify the code so that I could do this only by code?
I THINK you have some problem in displaying a remote image in uiimageview so i thing u should do that fashion.
NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://image.com/image.jpg"]] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
[image release];
[imageView release];
and i connected the UIImageView from the Interface builder
That was a mistake. If you do that, the image view pointed to by your image instance variable will be the wrong one - the one in the nib. You want it to be the one that you created in code.
So, make no connection from Interface Builder; in fact, delete the image view from Interface Builder so you don't confuse yourself. Make sure your instance variable is also a property:
#property (nonatomic, retain) UIImageView* image;
Synthesize the property:
#synthesize image;
Now your code will work:
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
self.image = [[UIImageView alloc] initWithImage:image1];
// no memory management needed if you're using ARC
[self.view addSubview:self.image];
You will need to play with the frame until the location is correct. Note that the frame will automatically be the same size as the image, by default.
you dont need to connect. This code will work without connecting. Leave IBOutlet out.
UIImage *someImage = [UIImage imageName:#"http://image.com/image.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:someImage];
[self.view addSubView:imageView];
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'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];
}
UIImage *image = [[UIImage alloc] initWithContentsOfFile:#"popup.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[image release];
[imageView release];
The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.
I'm tired.
initWithContentsOfFile requires the complete file path, not just the file name.
Replace the line where you initialize UIImage with the following:
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:#"popup" ofType:#"png"]];
A simpler way will be to use the imageNamed: method of the UIImage class
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popup.png"]];
And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.
Hope this helps!
Try to use:
UIImage *image = [UIImage imageNamed:#"popup.png"];
and, off course, get rid of the [image release]; because in this solution the image is autoreleased.
Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...
your imageView needs a frame.
imageView.frame = CGRectMake(0,0,320,480);
I know this question is old now, but some users can still benefit from an answer.
Like others have said, try using [UIImage imagedNamed:] instead of initWithContentsOfFile.
And the reason it is not showing up on screen is you did not set a location for the image. You can fix this by either setting a frame for the image view or setting its center at a single point. To place the image view in the center of the screen:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = [self.view center];
// or if you want to set a frame instead
// CGRect imageFrame = CGRectMake(x,y,width,height);
// [imageView setFrame:imageFrame];
[self.view addSubView:imageView];
Enjoy :)