美文网首页
iOS运行奔溃 -- Terminating app due t

iOS运行奔溃 -- Terminating app due t

作者: 字节码 | 来源:发表于2017-01-19 21:21 被阅读65次

    错误信息:
    Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [0 nan]'

    原因:CALayer的position位置中包含有不存在的数,说白了就是View.frame 计算时存在了问题,有的地方除以0了。

    当我打断点找到引发奔溃的那个控件时,打印它的Size信息(345.0, inf),其中高度为inf,也就是是高度计算的结果引发了奔溃的问题,因为服务器返回的图片宽度为0导致计算错误,也就是某个地方除以了0导致的, 当为0的时候判断下再处理即可解决

    // 计算collectionView的尺寸
        func caculatePicViewSize(imgList: [XYTrendImgItem]) -> CGSize {
            
            // 以内容的宽度为准,等比例计算高度
            if imgList.count == 0 {
                return CGSize.zero
            }
            
            if imgList.count == 1 {
                if let width = imgList.first?.imgSize.width, let height = imgList.first?.imgSize.height {
                    let oneImageHeight = contentViewWidth / width * height
                   return CGSize.init(width: contentViewWidth, height: oneImageHeight)
                }
            }
            
            let picViewW = contentViewWidth
            var picViewH : CGFloat = 0.0;
            var imgWidth : CGFloat = 0.0
            for item in imgList {
                imgWidth = item.imgSize.width
                // 计算每一个图片的等比例高度
                // 注意: 此处需要判断服务器返回的width是否为0 ,如果为0,就让其为内容视图的宽度,不然为0 时 使用除法会保存
                    if imgWidth == 0 {
                        imgWidth = contentViewWidth
                    }
                    let oneImageHeight = contentViewWidth / imgWidth * item.imgSize.height
                    picViewH += oneImageHeight
                
                print(imgWidth, oneImageHeight)
            }
            picViewH += CGFloat(imgList.count - 1) * SIZE_PICMARGIN
            print(picViewH, CGFloat(imgList.count - 1))
            return CGSize.init(width: picViewW, height: picViewH)
        }
    

    相关文章

      网友评论

          本文标题:iOS运行奔溃 -- Terminating app due t

          本文链接:https://www.haomeiwen.com/subject/lcaibttx.html