0

I essentially have a menu, where bunch of buttons are displayed and every time one of them is clicked it leads to same view that is displayed differently depending on the item selected from the menu, there is also an mp4 playing. Every time I go into one of the items (different view) or return to the menu, my memory increases and never drops down, the views are presented modally if that makes any difference. I'm guessing the creep is caused by the mp4, how could I solve this problem?

import Foundation
import UIKit
import AVKit
import AVFoundation

class menu : UIViewController {


var info : AVPlayer?
var count = 0

@IBAction func instrumentas2(sender: UIButton) {
    count = 2
}
@IBAction func instrumentas1(sender: UIButton) {
    count = 1
}
@IBAction func instrumentas0(sender: UIButton) {
    count = 0    }


override func viewWillAppear(animated: Bool) {


     let videoURL: NSURL = NSBundle.mainBundle().URLForResource("info2", withExtension: "mp4")!


        info = AVPlayer(URL: videoURL)
        info?.actionAtItemEnd = .None
        info?.muted = true



        let playerLayer = AVPlayerLayer(player: info)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer.zPosition = 1
        playerLayer.frame = CGRect(x:20.0, y: 703.0, width: 36.0, height: 36.0)


        view.layer.addSublayer(playerLayer)

        info?.play()

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "loopVideo",
            name: AVPlayerItemDidPlayToEndTimeNotification,
            object:nil)




}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name:AVPlayerItemDidPlayToEndTimeNotification, object: nil)
    let du:display = segue.destinationViewController as! display
    du.skaicius = count

}

func loopVideo() {
    info?.seekToTime(kCMTimeZero)
    info?.play()
}
}

I'm posting the whole thing because I honestly not sure what could be causing this.

Brian
  • 14,610
  • 7
  • 35
  • 43
snukumas
  • 75
  • 1
  • 1
  • 8

1 Answers1

0

This line of code:

 view.layer.addSublayer(playerLayer)

is adding sublayers. Are you ever removing them?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • i thought that they get removed when i segue to other view, if thats not the case how could i remove them? – snukumas Feb 11 '16 at 20:57
  • I have not experienced this problem, but try this question/answer: http://stackoverflow.com/questions/10789766/remove-all-sublayers-from-a-view – Dave2e Feb 11 '16 at 21:01
  • this is very useful, could you give me an example of how would this look implemented into my code, im not entirely sure i understand how is this supposed to be implemented, as in where would i put this line of code- self.view.layer.performSelector("removeFromSuperlayer") and what do i need to adjust for it to fit my code – snukumas Feb 11 '16 at 21:20