我使用這段代碼,如果一個iad加載失敗,它會查找admob。一切似乎工作正常,除了在儀器中,我注意到一個admob被稱爲任何時候大內存秒殺。在多次使用儀器之後,我只能得到一次內存泄漏,當一個admob被調用時,我肯定會發生內存泄漏。我看到一些人用admob談論內存泄漏,但我不確定這是否是固定的。ios iad和admob集成導致內存泄漏?
我的代碼看起來不錯嗎?如果希望這可以幫助別人,但我最終可能會將admob從我的應用程序中取出,因爲它似乎在一段時間後大幅度減慢了程序。我也沒有意識到sdk接近8mb。
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[bannerView_ removeFromSuperview];
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, -50.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)callAdMob {
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = @"";
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 50.0);
[UIView commitAnimations];
self.bannerIsVisible = NO;
NSLog(@"bannerview did not receive any banner due to %@", error);
[self callAdMob];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0.0, 367.0);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
}
您使用ARC嗎?如果沒有,它似乎沒有在任何地方釋放「bannerView_」或「adView」。 'addSubview'會保留你傳遞給它的視圖,所以你可以在把它交給'addSubview'後自由釋放視圖。 – 2012-02-16 20:02:43
是的,我正在使用ARC – user1108720 2012-02-16 20:04:16
只是爲了更新任何可能使用此代碼的人,在運行儀器時我只注意到了這個代碼的減速。我沒有注意到設備上的任何放緩。 – user1108720 2012-02-16 23:05:25