gpt4 book ai didi

swift - NSImagerepresentations.first 返回 nil。为什么?

转载 作者:行者123 更新时间:2023-11-30 13:06:53 28 4
gpt4 key购买 nike

在下面的代码中,我尝试将 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!
}

当它运行时,位被设置为零,我不明白为什么。有人能解释一下为什么吗?

最佳答案

经过仔细地反复使用 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!
}

关于swift - NSImagerepresentations.first 返回 nil。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39255530/

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