2

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?

4 Answers 4

7

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];
Sign up to request clarification or add additional context in comments.

4 Comments

Why haven't you used initWithImage ? and what is the purpose of using the NSData as used in your code ?
Please see third line i have used initWithImage method and as u want to receive an image from a url thats why u have to use dataWithContentsOfURL method to download a particular image in binary format.
Sorry i am a beginner. Can you tell me the advantage of downloading it in binary form (the way you have coded it) and the way shown in other peoples answers ? (I am a beginner so can you help me undestand)
Does the other given code(apart from my code) worked for u. if not then i think [UIImage imageName:@"image.png"] worked only for those images which are in Bundle or in project or local but if u want to display an image from web the u have to download synchronously using, dataWithContentOfUrl method.
3

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.

2 Comments

Does ARC comes with iOS 5 ?
Yes! And see my book to learn what it does for you: apeth.com/iOSBook/ch12.html#_memory_management
2

you dont need to connect. This code will work without connecting. Leave IBOutlet out.

2 Comments

You say that i need to remove IBOutlet and remove the UIImageView from the Interface Builder and the code will work ?
yes. you have all that you need: You create an imageView, set an image, set the frame and adding it to a view.
2
UIImage *someImage = [UIImage imageName:@"http://image.com/image.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:someImage];
[self.view addSubView:imageView];

2 Comments

You haven't specified the size of the UIImageView so will the image take the whole frame ?
it will automatically set it to the size of the image 'someImage' you are initing it with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.