NSImage representations.first 返回零。为什么?
NSImage representations.first returning nil. Why?
在下面的代码中,我试图将 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,我不明白为什么。有人可以解释为什么吗?
在 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!
}
在下面的代码中,我试图将 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,我不明白为什么。有人可以解释为什么吗?
在 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!
}