Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift get内存中加载本地gif警告_Ios_Swift_Gif - Fatal编程技术网

Ios 在swift get内存中加载本地gif警告

Ios 在swift get内存中加载本地gif警告,ios,swift,gif,Ios,Swift,Gif,我有一个可展开和折叠的表格视图,它在展开时显示多个GIF文件。在某个特定的时间,当我一次又一次地扩展多个部分时,当达到600 MB的空间时,它会崩溃并发出内存警告 我的实施: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let imageView = UIImageView() imageView.frame = C

我有一个可展开和折叠的表格视图,它在展开时显示多个GIF文件。在某个特定的时间,当我一次又一次地扩展多个部分时,当达到600 MB的空间时,它会崩溃并发出内存警告

我的实施:

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let imageView = UIImageView()
    imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 

    DispatchQueue.global(qos: .background).async {

        DispatchQueue.main.async {

           let imageData = try! Data(contentsOf: Bundle.main.url(forResource: "compile_animatiion", withExtension: "gif")!)
            //imggview.image = UIImage.gif(data: imageData)
            imageView.image =  UIImage.gifImageWithData(imageData) 
            cell.addSubview(imageView)
        }
    }

    cell.addSubview(imageView)
   }
我的代码使用UIImage+Gif类,您可以很容易地从git获得它。我需要准确的技术来避免这个内存警告


示例来源:drive.google.com/open?id=1tlvwaofwoaronf91yykutdyc0ilhazq

改进
1避免在方法
cellForRowAt
中创建无限图像视图
2将全局gif图像数据共享给所有重复使用的单元格

代码

private var _gifImageData:UIImage?
var gifImageData:UIImage! {
    get{
        if (_gifImageData != nil) {
            return _gifImageData
        }
        else{
            let imageData = try! Data(contentsOf: Bundle.main.url(forResource: "compile_animatiion", withExtension: "gif")!)
            _gifImageData = UIImage.gifImageWithData(imageData)
            return _gifImageData
        }
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
    let tagOfGifImageView = 1283
    if cell.viewWithTag(tagOfGifImageView) == nil {
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        imageView.tag = tagOfGifImageView
        imageView.image = self.gifImageData
        cell.addSubview(imageView)
    }

    return cell
}

我有44个独立的gif。同样的问题仍然存在:(