Created
May 2, 2016 12:56
-
-
Save dddnuts/ede04e65193d5df6f5b901c004adc7c3 to your computer and use it in GitHub Desktop.
Get sharable file URL from PHAsset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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