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
and BoardView.m file
Thank you in advance!
Regards,
Niklas
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:[COLOR="Red"]beigeImage[/COLOR]];
}else if (ran == 1) {
NSLog(@"Innuti 1");
bric2[xItr][yItr] = [[Bric alloc]initWithImage:[COLOR="Red"]brownImage[/COLOR]];
}else if (ran == 2) {
NSLog(@"Innuti 2");
bric2[xItr][yItr] = [[Bric alloc]initWithImage:[COLOR="Red"]greenImage[/COLOR]];
}else {
NSLog(@"Innuti 3");
bric2[xItr][yItr] = [[Bric alloc]initWithImage:[COLOR="Red"]orangeImage[/COLOR]];
}
ran = random() % 4;
}
}
//put the images on the screen...
}
@end
Thank you in advance!
Regards,
Niklas