UICollectionView内存泄漏(在单元格内部使用UIWebView或UIImageView)

时间:2016-08-24 16:47:29

标签: swift uiwebview uiimageview uicollectionview uicollectionviewcell

在我的应用程序中,我有一个UICollectionView,其中一些单元格包含来自网络的图像,一些单元格通过UIWebView显示Youtube视频。当我按Back通过NavigationController时,内存似乎每次都会越来越多。我试过禁用视频播放器,它有点帮助,但仍然......

CollectionViewController

    var listOfData = [SingleData]()
    var Data:[[String:AnyObject]]?{    
            didSet{
                for asset in Data! {
                        let info = SingleData.init(Data: asset)
                        listOfData.append(info)
                 }
            }
        }


    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        listOfData.removeAll()
        Data?.removeAll()
        imageCache.removeAllObjects()
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return  listOfData.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Data Single Cell", forIndexPath: indexPath) as! CollectionViewControllerCell
            cell.oneData = listOfData[indexPath.item] 
            return cell
    }

CollectionViewControllerCell

    @IBOutlet var videoWebView: UIWebView!
    @IBOutlet var singleDataImageView: UIImageView!

    var oneData:SingleData! {
        didSet{
            updateUI()
        }
    }

    func updateUI(){

        singleImageView.image = nil

        if (oneData.isVideo == true) {
            videoWebView.hidden = false
            singleDataImageView.hidden = true
            showVideo()
        } else {
            videoWebView.hidden = true
            singleDataImageView.hidden = false
            setImage(oneData.imageURL, singleDataImageView)
        }
    }

    func showVideo() {
        if let videoUrl = oneData.videoUrl {

            let replacedString = videoUrl.replace("watch?v=", withString: "embed/")
            videoWebView.scrollView.scrollEnabled = false
            videoWebView.scrollView.bounces = false
            videoWebView.allowsInlineMediaPlayback = true

            let embededHTML = "<html><head><title>.</title><style>body,html,iframe{margin:0;padding:0;}</style></head><body><iframe width=\"\(videoWebView.frame.width)\" height=\"\(videoWebView.frame.height)\" src=\"\(replacedString)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe></body></html>"

            videoWebView.loadHTMLString(embededHTML, baseURL:nil)

        }

    }


    func setImage(url : String?, _ imageView : UIImageView) {

        imageView.image = nil        
        imageUrlString = url


        if let cUrl = url {

            if let imageFromCache = imageCache.objectForKey(cUrl) as? UIImage {
                imageView.image = imageFromCache
                return
            }

            if let urlImage = NSURL(string: cUrl) {

                Request.sharedInstance.session.dataTaskWithURL(urlImage, completionHandler: { (data, response, error) in
                    if error != nil {
                        print(error)
                        return
                    }

                    dispatch_async(dispatch_get_main_queue(), {

                        let imageToCache = UIImage(data: data!)

                        if self.imageUrlString == url {
                            imageView.image = imageToCache
                        }

                        imageCache.setObject(imageToCache!, forKey: url!)
                    });

                }).resume()
            }
        }

    }

SingleData

class SingleData {

    var imageURL:String? = nil
    var videoUrl:String? = nil
    var isVideo:Bool? = false
    var dataAssets:Dictionary<String, AnyObject>?    

    init(Data : Dictionary<String, AnyObject>?){
        dataAssets = Data
        if Data!["MediaContent"]!["MediaType"] as? Int == 2 {
            isVideo = true
        }

        imageURL = dataAssets!["MediaContent"]!["FileUrl"] as? String
        videoUrl = dataAssets!["MediaContent"]!["FileName"] as? String
    }

}

0 个答案:

没有答案