0

So I used this code:

func videoSnapshot(filePathLocal: String) -> UIImage? {

    let vidURL = NSURL(fileURLWithPath:filePathLocal as String)
    let asset = AVURLAsset(URL: vidURL)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true

    let timestamp = CMTime(seconds: 1, preferredTimescale: 60)

    do {
        let imageRef = try generator.copyCGImageAtTime(timestamp, actualTime: nil)
        return UIImage(CGImage: imageRef)
    }
    catch
    {
        print("Image generation failed with error \(error)")
        return nil
    }
}

to get the UIImage of a snapshot from my video. I called this function like this:

let tempImg: UIImage = videoSnapshot(pathToFile)!

now I would like to upload this tempImg to my server, and to to this I need a path to this file - I will pass it later to the function that uploads data further. How can I get a temporary path to it and store it as a String or NSURL?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • 1
    get the image data using JPEG Data Representation http://stackoverflow.com/questions/29726643/how-to-compress-of-reduce-the-size-of-an-image-before-uploading-to-parse-as-pffi/29726675#29726675 and use NSData method writeToURL or writeToPath to save it to disk. You can save it at http://stackoverflow.com/questions/11897825/ios-temporary-folder-location/39318537#39318537 – Leo Dabus Sep 20 '16 at 19:37
  • Thanks Leo, I understand the first link, now I have the `UIImage` saved as a `NSData`, but I don't understand the 2nd link - how can I save this `NSData` to the temp directory? (I'm using Swift 2) – user3766930 Sep 20 '16 at 19:48
  • 1
    just use `yourData.writeToURL(destination: url, options: .atomic)`. You can create a new url for the destination using URL appendingPathComponent `tmpURL.appendingPathComponent("fileName.xxx")` – Leo Dabus Sep 20 '16 at 19:49
  • 1
    Thanks man, do you bother to submit it as an answer to this question so that I could accept it? – user3766930 Sep 20 '16 at 19:54
  • 1
    user3766930 You are welcome. You should update your Xcode to version 8. You can still use Swift legacy 2.3 on your projects if you don't want to use Swift 3 – Leo Dabus Sep 20 '16 at 19:55
  • I will post it later – Leo Dabus Sep 20 '16 at 19:56

1 Answers1

1

You have to get the image data using JPEG Representation method (check this so UIImageJPEGRepresentation answer) and use NSData method writeToURL or writeToPath to save it to disk. For temporary items you can create a destination url at the temporary folder url using URL appendingPathComponent method:

Swift 3 would look like this:

let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent("filename.jpg")
if let tempImg = videoSnapshot("filePathLocal"),
    let imgData = UIImageJPEGRepresentation(tempImg, 1) {
    do {
        try  imgData.write(to: destinationURL, options: .atomic)
        print("saved at:", destinationURL.path)
    } catch   {
        print(error.localizedDescription)
    }
}

Swift 2.3

if let tempImg = videoSnapshot("filePathLocal"),
    let imgData = UIImageJPEGRepresentation(tempImg, 1),
    let destinationURL = NSFileManager.defaultManager().temporaryDirectory.URLByAppendingPathComponent("filename.jpg")
    where imgData.writeToURL(destinationURL, atomically: true) {
    print("saved at:", destinationURL.path)
}
Community
  • 1
  • 1
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571