|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
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
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
Regards, Niklas |
|
|
|
0
|
|
|
#2 |
|
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...
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
|
|
#3 |
|
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 |
|
|
|
0
|
|
|
#4 |
|
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.
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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.







Linear Mode

