Swift中的iOS内存警告

Swift中的iOS内存警告,ios,iphone,swift,memory-management,Ios,Iphone,Swift,Memory Management,我使用计时器每0.1秒触发一个函数,以使用UIGraphicsGetImageFromCurrentImageContext获取UIImage,并将其分配给a@IBOutllet弱UImageView.image。(coverImageView) 以上所有代码都在ViewController.swift中进行测试 在函数运行期间内存增加是可以的,我想要的是它可以在我使计时器无效时释放内存 如何解决此问题?您正在启动图像上下文:UIGraphicsBeginImageContextWithOpti

我使用计时器每0.1秒触发一个函数,以使用UIGraphicsGetImageFromCurrentImageContext获取UIImage,并将其分配给a@IBOutllet弱UImageView.image。(coverImageView)

以上所有代码都在ViewController.swift中进行测试

在函数运行期间内存增加是可以的,我想要的是它可以在我使计时器无效时释放内存


如何解决此问题?

您正在启动图像上下文:
UIGraphicsBeginImageContextWithOptions
,但您永远不会结束它。您需要将
UIGraphicsEndImageContext()
添加到
getMaskImageFromPath
getOringinalImageFromPath
方法中:

func getMaskImageFromPath(points:[CGPoint], scale:CGFloat) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(coverImageView.bounds.size, false, 0.0)
    let radius:CGFloat = 10.0
    UIColor.blue.setFill()
    for point in points {
        let rect = CGRect(x: point.x - (scale*radius/2), y: point.y - (scale*radius/2), width: scale*radius, height: scale*radius)
        let path = UIBezierPath(ovalIn: rect)
        path.fill()
    }
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // Missing this line
    return image!
}

把你试过的代码放进去,我已经编辑过了,谢谢!在ARC中,对于要释放的某些变量,使用该引用的所有变量都需要超出范围。查看
coverImageView
将超出范围的位置。更好的方法是在需要时请求图像,而不是使用计时器。因此,当需要显示图像时,请检查图像是否已可用,否则请获取图像。您好,用户1046037,谢谢您的回复。实际上,当mapView进行任何区域更改时,我必须更新图像(并在更改期间不断更新图像),但是代理仅提供will change和Dod change。如果我能做得更好,谢谢你给我任何建议!运行循环保留了计时器。嗨,戴夫,在我修复并运行之后,它节省了大量内存!好吧,但在我不断启动并使计时器失效后,内存只会增加,而不会减少,我如何才能将其向前修正?或者给我指明问题所在,非常感谢!你还在收到内存警告吗?如果您只是查看内存工具是否报告为已使用状态,这通常会包括稍后将释放的内存。因此,我只是查看内存工具,正如您所说,我将继续检查它,谢谢!!
func getMaskImageFromPath(points:[CGPoint], scale:CGFloat) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(coverImageView.bounds.size, false, 0.0)
    let radius:CGFloat = 10.0
    UIColor.blue.setFill()
    for point in points {
        let rect = CGRect(x: point.x - (scale*radius/2), y: point.y - (scale*radius/2), width: scale*radius, height: scale*radius)
        let path = UIBezierPath(ovalIn: rect)
        path.fill()
    }
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // Missing this line
    return image!
}