-4

I have an application which has a collection view full of videos and images. When you tap on one of them you segue to a full screen to view the media. When I tap a image It works as needed. But when I run my application and tap one of those videos I get the error shown bellow:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x8000000000000010)

I get this here:

class AppDelegate: UIResponder, UIApplicationDelegate {

Is this possibly due to memory problems?

What is happening and how do i fix this?

I have looked here but was unable to solve it.

I have found that the fail actually occurs when the bellow function is called.

func getAndShowMedia(post: Post) {
    if post.media[numberMedia].image != nil {//here is still works, I am assuming this line is the line which actualy fails
        print("imageooooo")//When setting break point here it will crash
        mediaView.image = nil
        mediaView.image = (post.media[numberMedia].image)!
    } else {
        mediaView.layer.removeFromSuperlayer()
        let videoURL = post.media[numberMedia].videoURL
        let player = AVPlayer(url: videoURL! as URL)
        let playerLayer = AVPlayerLayer(player: player)
        print("videooooooo")
        playerLayer.frame = mediaView.bounds
        mediaView.layer.addSublayer(playerLayer)
        player.play()
    }
}

Here:

func loadOtherData() {        
    getAndShowMedia(post: selectedPost!)
}

2 Answers2

0

Please check your code and tell me where be crash.

I read your code and assume happening crash, i write be below point and check the debug mode.

  1. post.media[numberMedia].image you check is not nil please there case use if let statement.
  2. post.media[numberMedia].videoURL is geting in your model URL object that will happening crash.
  3. mediaView.layer.removeFromSuperlayer() i don't understand this code you have remove layer but you add sublayer mediaView.layer.addSublayer(playerLayer) then remove sublayer. Check your sublayer is available then remove sublayer, not remove mediaView layer.

use this code remove layer.

mediaView.layer.sublayers?.forEach({ $0.removeFromSuperlayer() })

I hope this code will be work.

Pradip Patel
  • 304
  • 1
  • 8
-1

From the Breakpoint menu in Xcode, press the '+' button at the bottom left then add an exception breakpoint. This will show you exactly where your app is crashing and you'll be able to debug it properly.

You have a few forced unwraps there that may be nil, you should avoid that.

RomuloVitoi
  • 117
  • 5