Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2008, 01:54 AM   #1 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 466
Red face Inserting An Image (Without IB)

I am looking on how i can add an image i have in my project into my application onto my view "MainView", through code.
I have a button, and i want to make the image appear when i press the button.

Other info:
- Image is 320x480
- Image name is "crack.png"
- Image is added into my project through XCode.
- I can make the image appear if i use the IB, i'm looking to do it through code.
- All i need to know is how to make the image appear, i have the action all set up.


Major beginner, any help?
Thanks, Steaps.

EDIT:
This always happens, you find the answer not after looking for an hour, but after you post a thread.
Code:
UIImage *image = [ UIImage imageNamed: @"crack.png" ];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: image ];
imageView.frame = CGRectMake(0.0f, 0.0f, 320, 480); // Set the frame in which the UIImage should be drawn in.
[ self addSubview: imageView ];

Last edited by Steaps; 12-20-2008 at 02:08 AM.
Steaps is offline   Reply With Quote
Old 12-20-2008, 02:13 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

FYI - When you create a UIImageView using initWithImage the initial size of the image view is the same as the image. So setting the frame in the code you posted is redundant in this case. But if you set the image view's frame to something else, the image would automatically be scaled for you to the specified size.
RickMaddy is offline   Reply With Quote
Old 12-20-2008, 02:18 AM   #3 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 466
Default

Quote:
Originally Posted by RickMaddy View Post
FYI - When you create a UIImageView using initWithImage the initial size of the image view is the same as the image. So setting the frame in the code you posted is redundant in this case. But if you set the image view's frame to something else, the image would automatically be scaled for you to the specified size.
Thank you, that will save a line of code .
Steaps is offline   Reply With Quote
Old 12-20-2008, 02:42 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Also, in the code you posted, you'll want to make sure you release the memory you allocated for imageView at the end of that little block. =)

Code:
[imageView release];
exorcyze is offline   Reply With Quote
Old 12-20-2008, 07:52 PM   #5 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 466
Default

Quote:
Originally Posted by exorcyze View Post
Also, in the code you posted, you'll want to make sure you release the memory you allocated for imageView at the end of that little block. =)

Code:
[imageView release];
True, thanks.
Steaps is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 257
23 members and 234 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Desert Diva, Dominus, glenn_sayers, HemiMG, JasonR, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:29 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0