I am trying to download files from the camera roll of an attached iPhone using Swift, AppKit and the ImageCaptureCore framework.
It seems to work fairly well when the camera roll contains a relatively small number of files – like < 100 or so, however it becomes very slow and unreliable with a very large number – like 1500 (ie. doesn't successfully download all of the files or sidecar files).
I have also tried requesting the each item individually, then requesting the next item once that download has been completed, however that takes much, much longer with a large number of images, and seems to also not be reliable.
I would appreciate any suggestions or advice.
import Foundation
import Cocoa
import ImageCaptureCore
func downloadCameraItems() {
guard let downloadLocation else { return }
let cd = cameraDevice.camDevice
let downloadOptions : [ICDownloadOption : Any] = [.downloadsDirectoryURL: downloadLocation, .overwrite: false, .sidecarFiles: true]
cameraItems.forEach { ci in
print("requesting download of \(ci.name ?? "")")
if let filename = ci.name {
let fileURL = downloadLocation.appendingPathComponent(filename)
if !FileManager.default.fileExists(atPath: fileURL.path) {
cd.requestDownloadFile(ci as! ICCameraFile, options: downloadOptions, downloadDelegate: self, didDownloadSelector: #selector(downloadItemCompleted), contextInfo: nil)
itemsRequested += 1
} else {
print("Request: Skipping, file already exists: \(fileURL.path)")
}
}
}
}
@objc func downloadItemCompleted(
_ file: ICCameraFile,
error: Error?,
options: [AnyHashable: Any]?,
contextInfo: UnsafeMutableRawPointer?
) {
print("*** Download: File downloaded - \(file.name ?? "")")
}