I have a View based application for iPhone/iPod that plays audio, having a VU meter showing the input dB level. This VU meter is animated using Core Animation.
The app runs fine, with the VU meter working great, having just one single View. But now, sadly, that I have inserted a Tab bar controller, to have another View with some internet content, I can’t get the VU meter to work on my first View, as before.
The app crashes (receiving the EXC_BAD_ACCESS message) because the UIView (that contains the VU meter .png file for Core Animation) is not being properly inserted during launching time as it did before as a View only application.
Here is my code, in the myAppDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
PlayerViewController *newController = [[PlayerViewController alloc] initWithNibName: @"PlayerView"
bundle: [NSBundle mainBundle]];
self.viewController = newController;
[newController release];
UIView *controllerView = [self.viewController view];
[self.window addSubview: controllerView];
[self.viewController addBargraphToView: controllerView];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And here is my PlayerViewController.m file, where I have the addBargraphToView method containing my image to be inserted. It remains invisible until the VU meter starts working:
- (void) addBargraphToView: (UIView *) parentView {
// static image for showing average level
UIImage *soundbarImage = [[UIImage imageNamed: @"vu-meter.png"] retain];
// background colors for generated image for showing peak level
self.peakClear = [UIColor clearColor];
self.peakGray = [UIColor lightGrayColor];
self.peakOrange = [UIColor orangeColor];
self.peakRed = [UIColor redColor];
levelMeter = [CALayer layer];
levelMeter.anchorPoint = CGPointMake (0.0, 20.0); // anchor to halfway up the left edge
levelMeter.frame = CGRectMake (15, 200, 0, kLevelMeterHeight); // set width to 0 to start to completely hide the bar graph segements
levelMeter.contents = (UIImage *) soundbarImage.CGImage;
peakLevelMeter = [CALayer layer];
peakLevelMeter.frame = CGRectMake (41, 233, 0, kLevelMeterHeight);
peakLevelMeter.anchorPoint = CGPointMake (0.5, 10.0);
peakLevelMeter.backgroundColor = peakGray.CGColor;
peakLevelMeter.bounds = CGRectMake (0, 0, 0, kLevelMeterHeight);
peakLevelMeter.contentsRect = CGRectMake (0, 0, 1.0, 1.0);
[parentView.layer addSublayer: levelMeter];
[parentView.layer addSublayer: peakLevelMeter];
[soundbarImage release];
}
How can I solve this problem? Should I insert the UIView image at some other point, and not during the application’s launch time?
Many thanks for your help, dear friends!
Move this call:
To -viewDidLoad inside your PlayerViewController implementation:
Edit
OK, I think the problem lies in double adding viewControllers. Make this the content of your application:didFinishLaunchingWithOptions: method: