ドキュメント ディレクトリに大きなサイズのビデオが保存されています。このビデオを取得して、最初の 5 バイトを削除したいと考えています。[NSData(contentsOf: videoURL)] を使用する 300 MB を超える大きなビデオ ファイルの場合、メモリの問題エラーが発生します。
Swift: Loading a large video file (over 700MB) into memory を調べたところ、大きなファイル には [InputStream] と [OutputStream] または [NSFileHandle] を使用する必要があることがわかりました。それの使い方?
サンプルコードを以下に示します。
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first{
let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(filePath)
do {
let videoData = try NSData(contentsOf: videoURL)
let mutabledata = videoData.mutableCopy() as! NSMutableData
mutabledata.replaceBytes(in: NSRange(location: 0, length: 5), withBytes: nil, length: 0)
}catch {
print("Error Writing video: \(error)")
}