Video TableViewCell Cause Memory Error in Swift

2019-07-23 16:14发布

I am trying to build a tableview cell that include videos in it. I am keeping my cell in nscache as I saw in another post in stack overflow. There goes my code:

Firstly I am calling this in viewDidload() and when index path.row %5=0

cache = NSMutableDictionary()

do {



            // create post request
            let url = NSURL(string: "http://molocate.elasticbeanstalk.com/video/api/explore/all/")!
            let request = NSMutableURLRequest(URL: url)
            request.HTTPMethod = "GET"

            // insert json data to the request
            //print(userToken)
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")
            request.addValue("Token "+userToken!, forHTTPHeaderField: "Authorization")


            //                let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in

            let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ (data, response, error) -> Void in

                dispatch_async(dispatch_get_main_queue(), {
                    //print(response)
                    //print(NSString(data: data!, encoding: NSUTF8StringEncoding))

                    if error != nil{
                        print("Error -> \(error)")

                        //return
                    }

                    do {
                        let result = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)

                        print("Result -> \(result)")
                        let videos = result["results"] as! NSArray


                        if result["next"] is NSNull {
                            print("next is null")
                            self.nextURL = nil
                        }else {
                            let nextStr = result["next"] as! String
                            print(nextStr)
                            self.nextURL = NSURL(string: nextStr)
                            }
                        for item in videos {

                            var videoStr = videoInf()
                            let urlString = item["video_url"] as! String
                            let url = NSURL(string: urlString)
                            videoStr.urlSta = url!





                            let username = item["current_username"] as! String
                            videoStr.username = username
                            self.videoArray.append(videoStr)


                        }





                    } catch {
                        print("Error -> \(error)")
                    }
                     self.tableView.reloadData()

                })


            }
            task.resume()





        } catch {
            print(error)


        }

Then, There is my code about my table view cell

var cell = videoCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myIdentifier")
     if((cache.objectForKey(NSString(format: "key%lu", indexPath.row))) != nil){
        cell = cache.objectForKey(NSString(format: "key%lu", indexPath.row)) as! videoCell
        cell.player.play()




     }else{



        cell.tableVideoURL = videoArray[indexPath.row].urlSta
        cell.exploreAddView()

     }   

But When I am at the 14th video my app is giving me a memory error and I could not to any utility of my app and the other cell have a place but they are not giving a video.

0条回答
登录 后发表回答