将视频保存到文档目录,而无需在swift

时间:2016-10-21 21:01:25

标签: swift3 file-manager

我有应用程序从照片库中选择视频并将其保存到文档目录当我选择一个视频并将其保存到文档目录时它工作正常但是当我选择两个视频或更多时我会崩溃我的应用程序请我需要正确将视频和图像保存到文档目录而不会使我的应用程序崩溃的方法

以下我的代码:

@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
 var getNumberFull = DealWithFileSystem.fetchNumberOfFilesInFolder(folderName: folderName + ".xcvg")

            for (_,asset) in self.dictionaryOfAssets.enumerated() {


                if asset.value.mediaType == .image {

                    if  let getNumberFromThumb = DealWithFileSystem.fetchNumberOfFilesInFolder(folderName: folderName) {
                        getNumberFull! = getNumberFromThumb
                        let filePathThumb = DealWithFileSystem.fetchFolderName(folderName: folderName)?.appendingPathComponent("Image \(getNumberFromThumb).png")
                        let fetchOption = PHImageRequestOptions()
                        fetchOption.isSynchronous = true
                        fetchOption.deliveryMode = .highQualityFormat
                        fetchOption.resizeMode = .exact
                        PHImageManager.default().requestImage(for: asset.value, targetSize: CGSize(width:79.0,height:79.0), contentMode: .aspectFit, options: fetchOption, resultHandler: { (img, nil) in
                            if let convert = UIImagePNGRepresentation(img!) {
                                try! convert.write(to: filePathThumb!)
                            }
                        })
                    }
                    let filePathFull = DealWithFileSystem.fetchFolderName(folderName: folderName + ".xcvg")?.appendingPathComponent("Image \(getNumberFull!).png")
                    let fetchOption = PHImageRequestOptions()
                    fetchOption.isSynchronous = true
                    PHImageManager.default().requestImageData(for: asset.value, options: nil, resultHandler: { (data, fetchOption, _, _) in
                        try! data?.write(to: filePathFull!)

                    })


                }else if asset.value.mediaType == .video {
                    let filePathThumb = DealWithFileSystem.fetchFolderName(folderName: folderName)?.appendingPathComponent("Video \(getNumberFull! + 1).jpg")
                    let fetchOption = PHImageRequestOptions()
                    fetchOption.deliveryMode = .highQualityFormat
                    fetchOption.resizeMode = .exact
                    fetchOption.isSynchronous = true
                    PHImageManager.default().requestImage(for: asset.value, targetSize: CGSize(width:79.0,height:79.0), contentMode: .aspectFit, options: fetchOption, resultHandler: { (img, info) in
                        if let convertImage = UIImageJPEGRepresentation(img!, 1.0) {
                            try! convertImage.write(to: filePathThumb!)
                        }
                    })
                    let filePathFull = DealWithFileSystem.fetchFolderName(folderName: folderName + ".xcvg")?.appendingPathComponent("Video \(getNumberFull! + 1).mp4")
                    PHImageManager.default().requestAVAsset(forVideo: asset.value, options: nil, resultHandler: { (AVAsset, AVAudio, info) in
                        let getUrlAsset = AVAsset as! AVURLAsset
                        if let convertVideo = NSData(contentsOf: getUrlAsset.url) {

                            convertVideo.write(to: filePathFull!, atomically: true)
                        }
                    })

                    getNumberFull! += 1
                }
            }

}

感谢前进

0 个答案:

没有答案