【发布时间】:2016-08-31 17:22:15
【问题描述】:
在下面的代码中,我试图将 NSImage 保存到磁盘。图片是从视频资产生成的。
@IBAction func chooseVideoFrame(sender: AnyObject) {
let playheadPosition = player2!.currentTime()
videoThumb = generateThumnail(videoURL.stringValue, fromTime: playheadPosition)
videoThumbnail.image = videoThumb
if let bits = videoThumb!.representations.first as? NSBitmapImageRep {
let data = bits.representationUsingType(.NSJPEGFileType, properties: [:])
theTempPath = (getDocumentsDirectory() as String) + "/thumbTemp.jpg"
data?.writeToFile(theTempPath!, atomically: false)
} else {
problemAlert("Video Thumbnail problem",info: "There's a problem with saving the video Thumbnail image.")
}
}
func generateThumnail(videoURL : String, fromTime:CMTime) -> NSImage {
let url = NSURL(string: videoURL)
let asset :AVAsset = AVAsset(URL:url!)
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
do { let thumbnail = try assetImgGenerate.copyCGImageAtTime(fromTime, actualTime: nil)
thumb = NSImage(CGImage: thumbnail, size: NSZeroSize)
} catch {
problemAlert("Could Not Generate Thumbnail",info: "There was a problem with making the video Thumbnail image.")
}
return thumb!
}
当它运行时,bits 被设置为 nil,我不明白为什么。谁能解释一下原因?
【问题讨论】: