首页 > 解决方案 > 如何在没有 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
)

标签: swiftswift5

解决方案


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

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

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

RunLoop.current.run()

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

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


推荐阅读