In the following code with I am trying to save an NSImage to disk. The image is generated from a video asset.
@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!
}
When it runs, bits is being set to nil and I don't understand why. Can someone explain why?
答案 0 :(得分:0)
在对xCode中的编译器进行一些小心的反复阅读并仔细阅读文档之后,我终于找到了可以按照我想要的方式运行的代码。
@IBAction func chooseVideoFrame(sender: AnyObject) {
// Get the playhead time and grab fram from that time
let playheadPosition = player2!.currentTime()
videoThumb = generateThumnail(videoURL.stringValue, fromTime: playheadPosition)
videoThumbnail.image = videoThumb
// Save the frame as a JPEG and save it to disk so it can be saved as
// as CKAsset later
if let tiffRep = videoThumb?.TIFFRepresentation {
let jpegImage = NSBitmapImageRep(data: tiffRep)!.representationUsingType(.NSJPEGFileType, properties: [:])!
theTempPath = (getDocumentsDirectory() as String) + "/thumbTemp.jpg"
jpegImage.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: CGSize(width: 768.0, height: 432.0))
} catch {
problemAlert("Could Not Generate Thumbnail",info: "There was a problem with making the video Thumbnail image.")
}
return thumb!
}