使用UIImagePNGRepresentation内存警告快速保存图像数组

时间:2016-01-27 08:31:07

标签: arrays swift save memory-warning uiimagepngrepresentation

我正在使用UIImagePNGRepresentation捕获UIView的随机大小/形状的部分并保存到磁盘。这些文件按预期保存,但是当我在设备上运行时,我得到了可怕的"收到内存警告",即使我正在做的是在循环中保存文件而不显示它们: / p>

UIGraphicsBeginImageContextWithOptions(bbox.size, false, 0)
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.25).CGColor)
CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, bbox.width, bbox.height))
CGContextFillPath(UIGraphicsGetCurrentContext())

let strokeImage = UIGraphicsGetImageFromCurrentImageContext()
let strokeFile = "stroke_\(strokeBezierArrayMemory.count).png"
let dataPng = UIImagePNGRepresentation(strokeImage)
let fileURL = folderURL!.URLByAppendingPathComponent(strokeFile)  //println("\(fileURL)")
if (dataPng.writeToURL(fileURL, options: .AtomicWrite, error: &error) ) {
  //println ("WROTE \(fileURL)")
} else {
  print (error)
}

我看过类似的帖子,但没有解决问题的答案。 This帖子建议使用AVFoundation,但我希望我能坚持使用UIImagePNGRepresentation。

我还尝试在autoreleasepool闭包中包装代码的NSData部分无济于事:

autoreleasepool({ () -> () in
  let strokeFile = "stroke_\(strokeBezierArrayMemory.count).png"
  let data = UIImagePNGRepresentation(strokeImage)
  let fileURL = folderURL!.URLByAppendingPathComponent(strokeFile)  //println("\(fileURL)")
  if (data.writeToURL(fileURL, options: .AtomicWrite, error: &error) ) {
    //println ("WROTE \(fileURL)")
  } else {
    print (error)
  }
})

有没有办法强制NSData在写入磁盘后释放每个dataPng?

0 个答案:

没有答案