这是我访问视频的方式:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
let video = NSData(contentsOfURL: videoURL)
}
}
video
我希望在我的控制器中显示用户(我不是指UIDocumentInteractionController
预览此内容)。我需要该视频的缩略图,然后将其分配给UIImageView
。有可能吗?
答案 0 :(得分:11)
最简单的答案:
import AVFoundation
private func thumbnailForVideoAtURL(url: NSURL) -> UIImage? {
let asset = AVAsset(URL: url)
let assetImageGenerator = AVAssetImageGenerator(asset: asset)
var time = asset.duration
time.value = min(time.value, 2)
do {
let imageRef = try assetImageGenerator.copyCGImageAtTime(time, actualTime: nil)
return UIImage(CGImage: imageRef)
} catch {
print("error")
return nil
}
}
答案 1 :(得分:3)
此处的SWIFT 3.0使用扩展程序
import ConfigParser
import os
class Configuration(ConfigParser.ConfigParser):
def __init__(self, config_file):
ConfigParser.ConfigParser.__init__(self)
self.read(config_file)
def saveSettings(self):
if not self.has_section('Section A'):
self.add_section('Section A')
self.set('Section A', 'Option 1', 'Value 1')
config_file = os.path.join(os.path.expanduser('~'),'config.ini' )
conf = Configuration(config_file)
conf.saveSettings()
然后,您可以随时随地访问 AVAsset / URL :
import Foundation
import UIKit
import MediaPlayer
import SystemConfiguration
extension AVAsset{
var videoThumbnail:UIImage?{
let assetImageGenerator = AVAssetImageGenerator(asset: self)
assetImageGenerator.appliesPreferredTrackTransform = true
var time = self.duration
time.value = min(time.value, 2)
do {
let imageRef = try assetImageGenerator.copyCGImage(at: time, actualTime: nil)
let thumbNail = UIImage.init(cgImage: imageRef)
print("Video Thumbnail genertated successfuly".DubugSuccess())
return thumbNail
} catch {
print("error getting thumbnail video".DubugError(),error.localizedDescription)
return nil
}
}
}
答案 2 :(得分:2)
您可以使用此方法生成缩略图:
import AVFoundation
func generateThumnail(url : NSURL) -> UIImage?{
let asset: AVAsset = AVAsset(URL: url)
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time : CMTime = CMTimeMake(1, 30)
let img : CGImageRef
do {
try img = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
let frameImg: UIImage = UIImage(CGImage: img)
return frameImg
} catch {
}
return nil
}
答案 3 :(得分:0)
回答AiOsN以及如何修复图像旋转的方法。 您只需要更改以下代码行:
return UIImage(CGImage: imageRef)
到这一个:
return UIImage(CGImage: imageRef scale: 1.0 , orientation: UIImageOrientation.Right)
,图像将显示正确的旋转。