如何删除沙箱没有xcode



之前我问了一个关于generateCGImagesAsynchronously的问题。值得庆幸的是,它得到了回答,并且工作得很好。

问题是它只有在xcode上作为Cocoa应用程序工作。我现在正试图将此逻辑移动到可执行的Swift包,但AVFoundation代码,如generateCGImagesAsynchronously,将不起作用。也就是说,不会引发错误,但这些函数似乎被模拟了。我猜想这个可能与我的包被沙盒有关?我能够从之前编写代码的Cocoa应用程序中删除沙盒,但我不知道如何为这个可执行文件做这件事。

我是Swift的新手,试图理解它,想到我想让我的代码做的事情取决于我正在使用的IDE,这有点令人沮丧。

如果有人能指出我在哪里阅读文档的方向,或其他一些来源,如何使程序不使用xcode,那将是伟大的。谢谢!

下面是我的代码:
import Darwin
import Foundation
import AppKit
import AVFoundation
import Cocoa

@discardableResult func writeCGImage(
_ image: CGImage,
to destinationURL: URL
) -> Bool {
guard let destination = CGImageDestinationCreateWithURL(
destinationURL as CFURL,
kUTTypePNG,
1,
nil
) else { return false }
CGImageDestinationAddImage(destination, image, nil)
return CGImageDestinationFinalize(destination)
}

func imageGenCompletionHandler(
requestedTime: CMTime,
image: CGImage?,
actualTime: CMTime,
result: AVAssetImageGenerator.Result,
error: Error?
) {
guard let image = image else { return }
let path = saveToPath.appendingPathComponent(
"img(actualTime).png"
)
writeCGImage(image, to: path)
}

let arguments: [String] = Array(CommandLine.arguments.dropFirst())
// For now, we assume the second arg, which is the
// path that the user wants us to save to, always exists.
let saveToPath = URL(fileURLWithPath: arguments[1], isDirectory: true)
let vidURL = URL(fileURLWithPath: arguments[0])
let vidAsset = AVAsset(url: vidURL)
let vidDuration = vidAsset.duration
let imageGen = AVAssetImageGenerator(asset: vidAsset)
var frameForTimes = [NSValue]()
let sampleCounts = 20
let totalTimeLength = Int(truncatingIfNeeded: vidDuration.value as Int64)
let steps = totalTimeLength / sampleCounts
for sampleCount in 0 ..< sampleCounts {
let cmTime = CMTimeMake(
value: Int64(sampleCount * steps),
timescale: Int32(vidDuration.timescale)
)
frameForTimes.append(NSValue(time: cmTime))
}
imageGen.generateCGImagesAsynchronously(
forTimes: frameForTimes,
completionHandler: imageGenCompletionHandler
)

正如我在对你之前问题的评论中所说,这与Xcode本身无关。Xcode只是为你生成大量的代码和构建命令。

macOS是一个复杂的操作系统,想要使用其更高级功能的程序必须遵循某些模式。其中一种模式称为运行循环。如果你创建了一个Cocoa应用,你可以免费获得这些东西。

由于您正在尝试执行一些异步操作,因此需要运行循环。附加这个应该可以工作:

RunLoop.current.run()

否则,程序将在主线程(代码)结束时终止。然而,运行循环导致程序运行一个循环并等待异步事件(例如,这也包括UI交互)发生。

请注意,插入同一行也可以解决其他问题中的问题。

最新更新