Trouble with UIImage in a class

Apprentice
Posts: 14
Joined: 2010.09
Post: #1
Hey guys,
I just recently posted a thread about having trouble modifying the frame/image of a UIImageView. Well thanks to much help I have figured out how to do this. Unfortunately I have hit another roadblock.

I am trying to write an app that keeps track of multiple BALL objects. My Ball class holds, amongst other things, a UIImage of the ball, and a CGRect to simulate the frame of the object.

EX:

Code:
#include <UIKit/UIKit.h>

@interface Ball: NSObject
{
    UIImage *ballImage;
    CGRect ballFrame;
}
@propety (nonatomic, retain) UIImage *ballImage;
@property CGRect ballFrame;

-(id) initWithX: (int) x andY: (int) y;

@end


Code:
#include "Ball.h"

@implementation Ball

@synthesize ballMovement;
@synthesize ballFrame;

-(id) initWithX: (int) x andY: (int) y
{
    [super init];
    
    ballFrame = CGRectMake(60, 60, x, y);
    ballImage = [[UIImage alloc] imageNamed: @"ball1.png"];

    return self;
}

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

@end

Now, I can successfully compile this code segment:

Code:
Ball *theBall = [[Ball alloc] initWithX: 65 andY: 65];
   UIImageView *ballImageView = [[UIImageView alloc] initWithFrame:
                                                  theBall.ballFrame];
   ballImageView.image = theBall.ballImage;
   [self.view addSubview: ballImageView];

...BUT, when the simulator runs, the program simply crashes and defaults back to the home screen.

I have tried this code without creating a Ball class. I create an UIImage and a frame. I then initialize a new UIImageView with the frame and add the image. Finally I tell the main view to add it as a sub view. This approach works flawlessly.

Is there any reason why this is working procedurally and not objectively (I think I got that right :/) ? I know that I can just implement the latter version and move on....but I am really trying to learn how to organize my images into class objects. PLEASE HELP

[edit AnotherJake] added code tags
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
When it crashes, Xcode should give you a backtrace showing the functions/methods that were called leading to the crash. What's in it?
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.09
Post: #3
I haven't seen anything like that (Im still trying to figure out the SDK as well). How would I enable that functionality?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
You have to run it in debug mode to get the backtrace generally. Make sure the configuration is set to "Debug" and not "Release" so that the code is generated in a way that the debugger understands, and then run it by using the Run -> Debugger menu.

Then when it crashes, it should show you the line that it crashed on and a stack trace of the functions that were called to get there.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.09
Post: #5
The stack trace I have gotten is:

0_TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION_
1 objc_exception_throw
2-[NSObject doesNotRecognizeSelector:]
3___forwarding___
4___forwarding__prep__0__
5-[Ball initWithX:andY:]

I synthesized my UIImage variable, but that does not seem to be the problem. I think the issue arises from inheriting from NSObject. Any ideas as to what could be holding me back on this one? Im going to try to Google the exception, but any help you could give would be great.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #6
(Sep 7, 2010 08:26 PM)Jmcclane Wrote:  
Code:
-(id) initWithX: (int) x andY: (int) y
{
    [super init];
    
    ballFrame = CGRectMake(60, 60, x, y);
    ballImage = [[UIImage alloc] imageNamed: @"ball1.png"];

    return self;
}

That should be

Code:
self = [super init];
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.09
Post: #7
Thanks for the edit...but Im still getting the same exception. Am I not inheriting the NSObject class correctly?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
UIImage doesn't respond to alloc.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.09
Post: #9
Oh man...you guys are awesome. Thanks!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  UIImage Dimensions? Jmcclane 6 5,515 Sep 2, 2010 06:27 PM
Last Post: AndyKorth
  UIImage question znstudios 2 2,488 Feb 24, 2010 01:55 PM
Last Post: znstudios
  Trouble With Class in Custom Framework Nick 8 3,640 Nov 26, 2005 01:11 PM
Last Post: Nick