Skip to content

Instantly share code, notes, and snippets.

@amosavian
Created September 28, 2016 15:23
Show Gist options
  • Save amosavian/37bd42c297da10d330ea544bd995e3a1 to your computer and use it in GitHub Desktop.
Save amosavian/37bd42c297da10d330ea544bd995e3a1 to your computer and use it in GitHub Desktop.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [pickedURL], options: nil)
if let asset = result.lastObject {
let options = PHImageRequestOptions()
options.isNetworkAccessAllowed = true
options.progressHandler = { (progress, _, _, _) in
self.setProgress(Float(progress), withTitle: "Fetching Image...")
}
PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: { (photoData, photoUTI, photoOrientation, photoInfo) -> Void in
let file = photoInfo?["PHImageFileURLKey"] as? URL
let fileName: String
if file?.deletingLastPathComponent().lastPathComponent == "Adjustments" {
fileName = ((file as NSURL?)?.deletingLastPathComponent?.deletingLastPathComponent().lastPathComponent ?? "IMG") + "." + (file?.pathExtension ?? ".jpg")
} else {
fileName = file?.lastPathComponent ?? "IMG.jpg"
}
let imagePath = (FileManager.default.cacheFolder.appendingPathComponent(fileName)).stringByUniqueFileName
let imageURL = URL(fileURLWithPath: imagePath)
let type = photoUTI ?? "public.jpeg"
if let photoData = photoData, let imageSource = CGImageSourceCreateWithData(photoData as CFData, nil), let imageDestination = CGImageDestinationCreateWithURL(URL(fileURLWithPath: imagePath) as CFURL, type as CFString, 1, nil) {
let cfImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, cfImageProperties)
CGImageDestinationFinalize(imageDestination)
let finalDest = self.provider.currentPath.appendingPathComponent(fileName)
self.provider.copyItem(localFile: imageURL, to: self.provider.fileByUniqueName(finalDest), completionHandler: nil)
}
})
}
/* iOS 7 ALAssetsLibrary old code
let library = ALAssetsLibrary()
ALAssetsLibrary.disableSharedPhotoStreamsSupport()
library.assetForURL(pickedURL, resultBlock: { (asset) -> Void in
if let imageRep = asset?.defaultRepresentation() {
let imagePath = (self.path.appendingPathComponent(imageRep.filename() ?? "IMG.jpg")).stringByUniqueFileName
let type = imageRep.UTI() ?? "public.jpeg"
if let imageDestination = CGImageDestinationCreateWithURL(NSURL(fileURLWithPath: imagePath), type, 1, nil) {
let imageProperties = asset.defaultRepresentation().metadata() ?? [:]
let image = asset.defaultRepresentation().fullResolutionImage().takeRetainedValue()
CGImageDestinationAddImage(imageDestination, image, imageProperties)
CGImageDestinationFinalize(imageDestination)
}
} else {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage, jpeg = UIImageJPEGRepresentation(image, 1.0) {
let imagePath = self.path.appendingPathComponent("IMG.jpg").stringByUniqueFileName
jpeg.writeToFile(imagePath, atomically: true)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.reloadTable()
}
}, failureBlock: { (error) -> Void in
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imagePath = self.path.appendingPathComponent("IMG").stringByUniqueFileName
let jpeg = UIImageJPEGRepresentation(image, 1.0)
jpeg?.writeToFile(imagePath, atomically: true)
}
})
*/
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment