Skip to content

Instantly share code, notes, and snippets.

@dddnuts
Created May 2, 2016 12:56
Show Gist options
  • Save dddnuts/ede04e65193d5df6f5b901c004adc7c3 to your computer and use it in GitHub Desktop.
Save dddnuts/ede04e65193d5df6f5b901c004adc7c3 to your computer and use it in GitHub Desktop.
Get sharable file URL from PHAsset
//
// DDDImageConverter.swift
// Unity-iPhone
//
// Created by dddnuts on 5/1/16.
//
//
import UIKit
import Photos
public class DDDImageConverter: NSObject {
public class func getImageURL(asset: PHAsset, size: CGSize) -> NSURL {
let manager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
options.synchronous = true
var publicURL = NSURL()
manager.requestImageForAsset(asset, targetSize: size, contentMode: .AspectFit, options: options, resultHandler: { result, info in
guard let image = result
else { return }
guard let imageData = UIImagePNGRepresentation(image)
else { return }
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd_HHmmss"
let dateString = dateFormatter.stringFromDate(NSDate())
let imageName = "dddimage_\(dateString).png"
let imagePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(imageName)
let success = imageData.writeToFile(imagePath, atomically: true)
if !success {
return
}
publicURL = NSURL(fileURLWithPath: imagePath)
})
return publicURL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment