首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从未调用过的AVAssetExportSession.requestExportSession回调(swift 3,iOS10)

从未调用过的AVAssetExportSession.requestExportSession回调(swift 3,iOS10)
EN

Stack Overflow用户
提问于 2016-12-01 01:58:26
回答 2查看 2.7K关注 0票数 1

下面的代码从不调用用于导出的回调。导出会话创建得很好。我看不到错误,也看不到进展。CPU为0%。我看不出有什么例外。状态为1(进行中),进度为0,错误为零。视频在画廊中播放。我可以成功地获取视频的图像。我已经将代码提取到单个UIViewController中进行测试,如下所示:

我使用iOS10.1.1和我在iPad上录制的视频进行了测试。

代码语言:javascript
复制
import UIKit
import Photos

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: Properties
@IBOutlet weak var button: UIButton!

// MARK: Actions
@IBAction func onPress(_ sender: UIButton) {
    requestGalleryPermission() {
        (_ hasPermission: Bool) in
        if hasPermission {
            // fetch the asset from Photos
            let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: ["9FFAD8B5-0941-4A95-830F-4ACFA563B71B/L0/001"], options: nil)

            // if we successfully fetched the asset...
            if let asset = fetchResult.firstObject {

                self.exportAsset(asset)
            }
        }
    }
}

func exportAsset(_ asset: PHAsset) {
    let tempFilename = "full_sized_image.mov"
    var tempURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(tempFilename)
    tempURL = tempURL.standardizedFileURL


    let options = PHVideoRequestOptions()
    options.deliveryMode = .highQualityFormat
    options.isNetworkAccessAllowed = true

    // remove any existing file at that location
    do {
        try FileManager.default.removeItem(at: tempURL)
    }
    catch {
        // most likely, the file didn't exist.  Don't sweat it
    }

    PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) {
        (exportSession: AVAssetExportSession?, _) in

        if exportSession == nil {
            print("COULD NOT CREATE EXPORT SESSION")
            return
        }

        exportSession!.outputURL = tempURL
        exportSession!.outputFileType = AVFileTypeQuickTimeMovie

        print("GOT EXPORT SESSION")
        exportSession!.exportAsynchronously() {
            print("COMPLETION HANDLER!!!!")
        }

        print("progress: \(exportSession!.progress)")
        print("error: \(exportSession!.error)")
        print("status: \(exportSession!.status.rawValue)")
    }
}

func requestGalleryPermission(_ completionHandler: @escaping (_ hasPermission: Bool) -> Void) {

    let authorizationStatus = PHPhotoLibrary.authorizationStatus()
    if authorizationStatus == .denied || authorizationStatus == .restricted {
        completionHandler(false)
    }
    else if authorizationStatus == .authorized {
        completionHandler(true)
    }
    else {
        // ask for authorization
        PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in

            // if the user gave us authorization...
            if status == .authorized {

                print("User gave authorization")

                completionHandler(true)
            }
            else {
                print("User denied authorization")

                completionHandler(false)
            }
        })
    }
}
}
EN

回答 2

Stack Overflow用户

发布于 2016-12-14 03:19:21

Swift 3对我来说很有吸引力!

代码语言:javascript
复制
func exportVideoAsset(_ asset: PHAsset) {
    let filename = UUID().uuidString.appending(".mp4") // setting random file name

    let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

    do {
        var fileurl = try documentsUrl.absoluteString.appending(filename).asURL()
        print("exporting video to ", fileurl)
        fileurl = fileurl.standardizedFileURL


        let options = PHVideoRequestOptions()
        options.deliveryMode = .highQualityFormat
        options.isNetworkAccessAllowed = true

        // remove any existing file at that location
        do {
            try FileManager.default.removeItem(at: fileurl)
        }
        catch {
            // most likely, the file didn't exist.  Don't sweat it
        }

        PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality) {
            (exportSession: AVAssetExportSession?, _) in

            if exportSession == nil {
                print("COULD NOT CREATE EXPORT SESSION")
                return
            }

            exportSession!.outputURL = fileurl
            exportSession!.outputFileType = AVFileTypeMPEG4 //file type encode goes here, you can change it for other types

            print("GOT EXPORT SESSION")
            exportSession!.exportAsynchronously() {
                 print("EXPORT DONE")
            }

            print("progress: \(exportSession!.progress)")
            print("error: \(exportSession!.error)")
            print("status: \(exportSession!.status.rawValue)")
        }
    }
    catch {
        // something may happend here, like no disk space
    }
}
票数 8
EN

Stack Overflow用户

发布于 2021-04-02 17:37:05

有时iOS会导致AVAssetExportSession的状态在.waiting上卡住

这似乎是在几次失败的导出尝试之后发生的。后续导出永远不会启动。

重新启动设备会清除导出队列,通常会修复该问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40895132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档