问题描述
我希望改善我的应用程序的内存使用情况,并注意到使用 AdMob 对我的应用程序产生了不利影响。
具体来说,使用此代码:
override func viewDidLoad() {
super.viewDidLoad()
multipleAdsOptions.numberOfAds = 5
let imageAdOptions = GADNativeAdImageAdLoaderOptions()
adLoader = GADAdLoader(adUnitID: "ca-app-pub-xxxxxxxx",rootViewController: self,adTypes: [GADAdLoaderAdType.native],options: [multipleAdsOptions,imageAdOptions])
adLoader.delegate = self
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ kGADSimulatorID.description() ]
DispatchQueue.main.async {
self.adLoader.load(GADRequest())
}
.. .. }
我注意到每次在我的应用程序中为这段代码触发 viewDidLoad 时,它使用的内存增加了大约 10-15mb。如果我快速点击进入和退出此屏幕(这实际上是一个常见操作),我可以根据需要尽可能提高 MB 使用率,直到我停止为止。我注意到这行代码当然是罪魁祸首:
self.adLoader.load(GADRequest())
如果我将其注释掉,我的应用程序将保持在 50 mb 的大小,甚至总体上看起来运行得更好。 我在这里使用 AdMob 错了吗?我应该加载一次广告并且不再在我的 viewdidload 中调用 load 吗?这是一个访问量很大的屏幕,我的测试用户报告应用在重新启动之前运行缓慢。
这是我的主屏幕(根视图控制器),它包含一个产品提要,我还有一个 AdvertisementFeedCell,如果有意义的话,我在此提要中每 5 个项目加载一个广告。
class AdvertisementFeedCell: UITableViewCell {
@IBOutlet var adMedia: GADNativeAdView!
@IBOutlet var adHeadline: UILabel!
@IBOutlet var unifiedNativeAdView: GADNativeAdView!
override func awakeFromNib() {
super.awakeFromNib()
}
}
当我填充表格时,我为广告每 5 个点插入一个新项目:
let count = self.feedItems.count / 5
//Insert an ad every 5
if (count > 0)
{
for i in 1...count
{
let adFeedItem = FeedItem()
adFeedItem.isAdvertisement = true
self.feedItems.insert(adFeedItem,at: i * 5)
}
}
self.reloadTable()
当我的 tableview 填充数据时,我检查该项目是否属于广告类型
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let feedItem = feedItems[indexPath.row]
if (feedItem.isAdvertisement)
{
let cell:AdvertisementFeedCell = tableView.dequeueReusableCell(withIdentifier: "adCell")! as! AdvertisementFeedCell
if (ads.count > 0 && indexPath.row > 1)
{
let nativeAd = ads[(indexPath.row / 5) - 1]
cell.unifiedNativeAdView.nativeAd = nativeAd
cell.adHeadline.text = nativeAd.headline!
//If there is an icon,use that
if (nativeAd.icon != nil)
{
(cell.unifiedNativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
cell.unifiedNativeAdView.mediaView?.isHidden = true
cell.unifiedNativeAdView.iconView?.isHidden = false
}
else {
//Otherwise,Add Media and hide icon
cell.unifiedNativeAdView.mediaView?.contentMode = UIViewContentMode.scaleToFill
cell.unifiedNativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
cell.unifiedNativeAdView.mediaView?.isHidden = false
cell.unifiedNativeAdView.iconView?.isHidden = true
}
cell.frame = CGRect(x: 0,y: 0,width: 375,height: 108 )
//Add body text if available
(cell.unifiedNativeAdView.bodyView as? UILabel)?.text = nativeAd.body
cell.unifiedNativeAdView.bodyView?.isHidden = nativeAd.body == nil
cell.clipsToBounds = true
cell.unifiedNativeAdView.clipsToBounds = true
}
return cell
}
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)