Photos framework: Connection to assetsd was interr

2020-06-16 03:20发布

问题:

I am getting this error when I am trying to play multiple videos using this swift library (https://github.com/piemonte/player). Not sure if it's related to that player, or to the Photos framework or what though.

What happens is, I have a view that will display either a photo or a video. Everything works fine a few times until a few videos have played and then this message will pop up, followed by all the videos not being able to play and in their place you just see a black screen, and then I get a memory usage error.

I am using a library called SwipeView and here is some relevant code which may be helpful.

func swipeView(swipeView: SwipeView!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! {
    let asset: PHAsset = self.photosAsset[index] as PHAsset

    // Create options for retrieving image (Degrades quality if using .Fast)
    //        let imageOptions = PHImageRequestOptions()
    //        imageOptions.resizeMode = PHImageRequestOptionsResizeMode.Fast
    var imageView: UIImageView!

    let screenSize: CGSize = UIScreen.mainScreen().bounds.size
    let targetSize = CGSizeMake(screenSize.width, screenSize.height)

    var options = PHImageRequestOptions()
    options.resizeMode = PHImageRequestOptionsResizeMode.Exact
    options.synchronous = true

    if (asset.mediaType == PHAssetMediaType.Image) {
        PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options, resultHandler: {(result, info) in
            if (result.size.width > 200) {
                imageView = UIImageView(image: result)
            }
        })

        return imageView
    } else if (asset.mediaType == PHAssetMediaType.Video) {
        self.currentlyPlaying = Player()


        PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {result, audio, info in
            self.currentlyPlaying.delegate = self
            self.currentlyPlaying.playbackLoops = true
            self.addChildViewController(self.currentlyPlaying)
            self.currentlyPlaying.didMoveToParentViewController(self)

            var t = result as AVURLAsset
            var url = t.valueForKey("URL") as NSURL
            var urlString = url.absoluteString

            self.currentlyPlaying.path = urlString
        })

        return self.currentlyPlaying.view
    }
    return UIView()
}


    func swipeViewItemSize(swipeView: SwipeView!) -> CGSize {
    return self.swipeView.bounds.size;
}

func swipeView(swipeView: SwipeView!, didSelectItemAtIndex index: Int) {
    self.currentlyPlaying.playFromBeginning()
}

func swipeViewCurrentItemIndexDidChange(swipeView: SwipeView!) {
    self.currentlyPlaying.stop()
}

Any thoughts would be great.

回答1:

I had this same "Connection to assetsd was interrupted or assetsd died" error.

Usually followed by a memory warning. I tried to find retain cycles, but it wasn't it.

The problem for me had to do with the known fact that the resultHandler block of any PHImageManager request...() is not called on the main queue.

So we cannot run UIView code directly within those blocks without asking for trouble later on in the app life.

To fix this we may for instance run all UIView code that would be executed within the scope of the resultHandler block inside a dispatch_async(dispatch_get_main_queue(), block)

I was having this problem because I did not realize that one of the functions that I was calling within one of the resultHandler.s of my app did eventually call some UIView code down the line. And so I was not forwarding that call to the main thread.

Hope this helps.



回答2:

The issue is occurring because of the TargetSize only. The PHImageManagerMaximumSize option is the culprit. Set the CGSIZE as you want for the imageview.



回答3:

Your problem might be that you create a new Player-object everytime you start playing a video:

 self.currentlyPlaying = Player()

I would recommend you to either remove it after the file has finished playing or to create one player which you will reuse. Otherwise it will stay in the memory.v