8

当我尝试使用这个 swift 库 ( https://github.com/piemonte/player ) 播放多个视频时出现此错误。不确定它是否与该播放器有关,或者与照片框架或其他什么有关。

发生的情况是,我有一个可以显示照片或视频的视图。一切正常几次,直到播放了几个视频,然后会弹出此消息,然后所有视频都无法播放,而在它们的位置,您只会看到黑屏,然后出现内存使用错误。

我正在使用一个名为 SwipeView 的库,这里有一些可能会有所帮助的相关代码。

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()
}

任何想法都会很棒。

4

3 回答 3

18

我有同样的“与 assetsd 的连接被中断或 assetsd 死亡”错误。

通常紧随其后的是内存警告。我试图找到保留周期,但不是这样。

对我来说,问题与已知事实有关,即任何PHImageManager的resultHandler块都没有在主队列上调用。 request...()

所以我们不能直接在这些块中运行UIView代码而不在应用程序生命的后期提出问题。

为了解决这个问题,我们可以运行所有UIView代码,这些代码将在resultHandler块的范围内执行dispatch_async(dispatch_get_main_queue(), block)

我遇到了这个问题,因为我没有意识到我在我的应用程序的一个resultHandler .s 中调用的函数之一最终确实调用了一些UIView代码。所以我没有将该调用转发到主线程。

希望这可以帮助。

于 2015-04-18T09:33:49.650 回答
3

仅由于 TargetSize 会出现此问题。PHImageManagerMaximumSize 选项是罪魁祸首。根据需要为图像视图设置 CGSIZE。

于 2018-09-07T14:58:56.380 回答
0

您的问题可能是每次开始播放视频时都会创建一个新的 Player-object:

 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

于 2015-01-21T10:29:31.863 回答