Search code examples
swiftcocoansimage

NSImage representations.first returning nil. Why?


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?


Solution

  • After some careful back and forth with the complier in xCode and close reading of the documentation, I did finally arrive at code that works as I want it to.

    @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!
        }