gpt4 book ai didi

ios - 快速保存电影

转载 作者:行者123 更新时间:2023-11-30 14:19:00 25 4
gpt4 key购买 nike

我使用以下代码并没有成功保存电影请告诉我哪里错了:

在上面我使用了,

import UIKit
import MobileCoreServices
import Foundation

class RecordingViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate

代码本身是通过按钮激活的,

//Recording a video
@IBAction func recordANewVideoMessage(sender: UIButton) {

//Initiate camera
let picker = UIImagePickerController()
picker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.Camera)
{

picker.sourceType = .Camera

if let availableTypes = UIImagePickerController.availableMediaTypesForSourceType(.Camera) {


if (availableTypes as NSArray).containsObject(kUTTypeMovie) {

/* Set up the camera defaults, movie. no editing, 3 minutes max - consider presenting time during recording, medium quality */
picker.mediaTypes = [kUTTypeMovie]
picker.allowsEditing = false
picker.videoMaximumDuration = 180 // Perhaps reduce 180 to 120
picker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
presentViewController(picker, animated: true, completion: nil) // presents the camera for the user


}
}
}



//In case the user cancels the movie
func imagePickerControllerDidCancel(UIImagePickerController) {

presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {


let uniqueVideoName = "/temporaryVideoName" //will be altered when we have something unique for the user

var video = info[UIImagePickerControllerReferenceURL] as! NSURL

let urlVideo = video.URLByAppendingPathComponent("\(uniqueVideoName)"+".mpeg4").absoluteString


if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlVideo))
{
UISaveVideoAtPathToSavedPhotosAlbum(urlVideo, nil, nil, nil)
}

presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

}

}

最佳答案

这是一种快速将视频保存到相机胶卷的方法:

import Photos

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)

if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
if let phAsset = fetchResult.firstObject as? PHAsset {
PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
if let asset = asset as? AVURLAsset {
let videoData = NSData(contentsOfURL: asset.URL)

// write the video to the temp directory
let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
let videoURL = NSURL(fileURLWithPath: videoPath)
videoData?.writeToURL(videoURL, atomically: true)

let assetLibrary = ALAssetsLibrary()
assetLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (url, error) -> Void in
if error == nil {
print("saved")
}
else {
print(error)
}
})
}
})
}
}
}

关于ios - 快速保存电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com