1

UIView のランダムなサイズ/形状のセクションをキャプチャし、UIImagePNGRepresentation を使用してディスクに保存しています。ファイルは期待どおりに保存されますが、デバイスで実行すると、ファイルをループで保存して表示しないだけでも、恐ろしい「受信メモリ警告」が表示されます。

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)
}

同様の投稿を見たことがありますが、私の問題を解決する答えはありません。 この投稿では AVFoundation の使用を提案していますが、UIImagePNGRepresentation に固執できることを願っています。

また、コードの NSData 部分を autoreleasepool クロージャーでラップしようとしましたが、役に立ちませんでした。

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)
  }
})

ディスクに書き込まれた各 dataPng を NSData に強制的に解放させる方法はありますか?

4

0 に答える 0