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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 05-28-2009, 10:32 AM   #1 (permalink)
situee.blogspot.com
 
Join Date: May 2009
Location: Guangzhou, China
Posts: 26
situee is on a distinguished road
Default Need help: add UIImageView as a subview

I simply want to add a UIImageView to my view in a view-based application.
but the picture does not show up. only black screen and a window "GDB loading stack frames..." comes out.

Thank you for reading this post and help me with this problem.
// File name: AddSubviewViewController.h
#import <UIKit/UIKit.h>
@interface AddSubviewViewController : UIViewController {
UIImageView *myImageView;
}
@property (nonatomic, retain) UIImageView *myImageView;
@end

// AddSubviewViewController.m
#import "AddSubviewViewController.h"
@implementation AddSubviewViewController
@synthesize myImageView;

- (void)loadView {}

- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banana1.png"]];
myImageView = imageView;
myImageView.frame = CGRectMake(0,0,61,80);
[imageView release];
[self.view addSubview:myImageView];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)dealloc {
[super dealloc];
}
@end

Last edited by situee; 05-28-2009 at 10:57 AM.
situee is offline   Reply With Quote
Old 05-29-2009, 03:29 AM   #2 (permalink)
Registered Member
 
Join Date: May 2009
Location: Albi, France
Age: 24
Posts: 45
dragon_tenvel is on a distinguished road
Send a message via MSN to dragon_tenvel
Default

Since nobody replied, I can try a few things :

Are you sure you added the banana1.png to your project?

Instead of the line
Code:
myImageView = imageView;
Try to put :

Code:
self.myImageView = imageView;
If it doesn't work, why don't you create a nib from interface Builder and init your view controller with this nib?
dragon_tenvel is offline   Reply With Quote
Old 05-29-2009, 09:02 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 133
Sicga is on a distinguished road
Default

This is the code I've used to add subviews to an imageView. I'm no expert but maybe it might help you.


UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 43.0f,261.0f, 276.0f)];


[subview setImage:[UIImage imageNamed:@"banana.png"]];
[imageView addSubview:subview];
[subview release];
Sicga is offline   Reply With Quote
Old 05-29-2009, 12:38 PM   #4 (permalink)
situee.blogspot.com
 
Join Date: May 2009
Location: Guangzhou, China
Posts: 26
situee is on a distinguished road
Default

dragon_tenvel , Sicga , thank you very much for the reply

I figured out the problem. I removed the empty method "loadView" and it works now. Maybe there is sth overrided by the empty method

Actually I don't know what's the difference between putting codes in
loadView and viewDidLoad.

but I still have a question on when I can release sth allocated by [class-name alloc]. sometime the program terminated when I try to release something. should I push all the releases in the dealloc method?
situee is offline   Reply With Quote
Old 05-29-2009, 01:27 PM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 133
Sicga is on a distinguished road
Default

From what I understand, anything you @synthesize in your implementation file (.m) you release in -(void)dealloc, I assume that is what you mean by [class.name alloc], anything you initialise in a function such as the viewDidLoad etc e.g NSMutableArray *array the contents of which are passed to the synthesized variable is normally released in that function, usually just before [super viewDidLoad];

Here's an example:

in .h file

@interface RootController: UITableViewController
{
NSArray *timezones
}

in .m file

@implementation RootController
@synthesize timezones;

-(void)viewDidLoad
NSMutableArray *array = [[NSMutableArray alloc] init];
self.timezones = array;
[array release];
[super viewDidLoad];
}

-(void)dealloc {
[timezones release];
[super dealloc];
}

I'm not 100% sure of this but that seems to be what more experienced iPhone programmers do.
Sicga is offline   Reply With Quote
Old 05-29-2009, 02:31 PM   #6 (permalink)
situee.blogspot.com
 
Join Date: May 2009
Location: Guangzhou, China
Posts: 26
situee is on a distinguished road
Default

Thank you! your comments really help me a lot to understand when to release. I found that in some sample codes from Apple there is no [super viewDidLoad] in the viewDidLoad method. It should be fine releasing instances at the end of the function where they were initialized.

Quote:
Originally Posted by Sicga View Post
From what I understand, anything you @synthesize in your implementation file (.m) you release in -(void)dealloc, I assume that is what you mean by [class.name alloc], anything you initialise in a function such as the viewDidLoad etc e.g NSMutableArray *array the contents of which are passed to the synthesized variable is normally released in that function, usually just before [super viewDidLoad];

Here's an example:

in .h file

@interface RootController: UITableViewController
{
NSArray *timezones
}

in .m file

@implementation RootController
@synthesize timezones;

-(void)viewDidLoad
NSMutableArray *array = [[NSMutableArray alloc] init];
self.timezones = array;
[array release];
[super viewDidLoad];
}

-(void)dealloc {
[timezones release];
[super dealloc];
}

I'm not 100% sure of this but that seems to be what more experienced iPhone programmers do.
situee is offline   Reply With Quote
Old 06-02-2009, 03:32 AM   #7 (permalink)
Registered Member
 
Join Date: May 2009
Location: Albi, France
Age: 24
Posts: 45
dragon_tenvel is on a distinguished road
Send a message via MSN to dragon_tenvel
Default

If you try to release an autorelease object your programm will quit. For example, don't try to release an NSString object
dragon_tenvel is offline   Reply With Quote
Reply

Bookmarks

Tags
subview, uiimageview

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: 342
5 members and 337 guests
givensur, ipodphone, jbro, mer10, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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