【问题标题】:NSImage representations.first returning nil. Why?NSImage representations.first 返回零。为什么?
【发布时间】: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,我不明白为什么。谁能解释一下原因?

【问题讨论】:

    标签: swift cocoa nsimage


    【解决方案1】:

    在与 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!
        }
    

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      相关资源
      最近更新 更多