Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 27, 2009, 10:44 AM   #1
Nicsoft
macrumors member
 
Join Date: Oct 2009
Location: Stockholm
Scope of variables? Doesn't manage to instantiate variables properly

Hello,

Another beginner's question...

I am using Interface Builder to create a UIView. I want to instantiate some UIImages by loading images from file before drawRect is called.

I am trying to do this in the awakeFromNib method. The methods is called before drawRect, but it seems like the UIImages is not properly instantiated since the application crasches as soon as I am trying to use the images.

If I instantiate the UIImages in the beginning of drawRect, no problem occurs.

I did copy/paste the important parts of the code below.

I include my BoardView.h

Code:
@interface BoardView : UIView {

	Bric *brics[13][9];
	UIImage *orangeImage;
	UIImage *brownImage;
	UIImage *greenImage;
	UIImage *beigeImage;
	
}

- (id)initWithFrame:(CGRect)frame;

- (void)awakeFromNib;

@end
and BoardView.m file

Code:
@implementation BoardView
- (void)awakeFromNib{
	NSLog(@"Inside awakeFromNib");
	beigeImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"beige.png" ofType:nil]];
	brownImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"brown.png" ofType:nil]];
	greenImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"green.png" ofType:nil]];
	orangeImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"orange.png" ofType:nil]];
		
}

- (void)drawRect:(CGRect)rect {

        CGPoint cp2 = CGPointMake(x2, 20);

	Bric *bric2[13][9];
		
	srandom(time(0));
	int ran = random() % 4;


       for (int xItr = 0; xItr < 13; xItr++) {
		for (int yItr=0; yItr < 9; yItr++) {	
			NSLog(@"Integer %i", ran);
			if (ran == 0) {
				NSLog(@"Innuti 0");
				bric2[xItr][yItr] = [[Bric alloc]initWithImage:beigeImage];
			}else if (ran == 1) {				
				NSLog(@"Innuti 1");
				bric2[xItr][yItr] = [[Bric alloc]initWithImage:brownImage];
			}else if (ran == 2) {
				NSLog(@"Innuti 2");
				bric2[xItr][yItr] = [[Bric alloc]initWithImage:greenImage];
			}else {
				NSLog(@"Innuti 3");
				bric2[xItr][yItr] = [[Bric alloc]initWithImage:orangeImage];
			}
			
			ran = random() % 4;
		}
	}

//put the images on the screen...
	
}

@end
Thank you in advance!

Regards,
Niklas
Nicsoft is offline   0 Reply With Quote
Old Oct 27, 2009, 11:13 AM   #2
robbieduncan
Demi-God (Moderator)
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Niklas go and read the Memory Management Guide for Cocoa. As a hint you are not using a method with init or copy in the name so the objects are autoreleased...
robbieduncan is offline   0 Reply With Quote
Old Oct 28, 2009, 03:37 AM   #3
Nicsoft
Thread Starter
macrumors member
 
Join Date: Oct 2009
Location: Stockholm
Thanks! Actually, I did read it, but unfortunately, it's not sitting in my back-bone yet...

So, the solution to the problem is that I didn't retain (or copy) the image, just stored it as a weak reference and, hence, it is autoreleased when the method ends. Hope I got it right there, please correct me if not.

In order to make it work I changed each row similar to this

beigeImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"beige.png" ofType:nil]];

to

beigeImage = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"beige.png" ofType:nil]] retain];

If you want to keep a copy of the image, just use "copy" instead of "retain" (and then changes to the original image want effect the copy).

Hope it helps someone else out there.

/Niklas
Nicsoft is offline   0 Reply With Quote
Old Oct 28, 2009, 04:20 AM   #4
robbieduncan
Demi-God (Moderator)
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Nicsoft View Post
So, the solution to the problem is that I didn't retain (or copy) the image, just stored it as a weak reference and, hence, it is autoreleased when the method ends.
Mostly right. You could also use an init method to get a retained object. And autoreleased objects probably live longer than that method, but not longer than the current iteration of the runloop.
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
File manager to move MP3s Jimmy Guphanti Jailbreaks and iOS Hacks 4 Apr 23, 2012 04:48 AM
Heroes of Newerth on sale for $10 Tinototem Mac and PC Games 34 Aug 19, 2011 04:34 PM
R-statistical: Changing categorical variables from text to numbers Erniecranks Mac Programming 6 May 8, 2011 07:30 PM
Portion of screen doesn't respond to touch gwass iPhone Tips, Help and Troubleshooting 41 Apr 12, 2010 07:43 PM
Understanding the scope of variables imPoddible iPhone/iPad Programming 0 Nov 24, 2008 12:17 PM


All times are GMT -5. The time now is 04:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC