Created
August 10, 2016 10:30
-
-
Save amosavian/093057412b34a308eec371d4469cccc6 to your computer and use it in GitHub Desktop.
imagePicker delegate implementation
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
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { | |
if let pickedURL = info[UIImagePickerControllerReferenceURL] as? NSURL { | |
let result = PHAsset.fetchAssetsWithALAssetURLs([pickedURL], options: nil) | |
if let asset = result.lastObject as? PHAsset { | |
let options = PHImageRequestOptions() | |
options.networkAccessAllowed = true | |
options.progressHandler = { (progress, _, _, _) in | |
self.setProgress(Float(progress), withTitle: "Fetching Image...") | |
} | |
PHImageManager.defaultManager().requestImageDataForAsset(asset, options: options, resultHandler: { (photoData, photoUTI, photoOrientation, photoInfo) -> Void in | |
let file = photoInfo?["PHImageFileURLKey"] as? NSURL | |
let fileName: String | |
if file?.URLByDeletingLastPathComponent?.lastPathComponent == "Adjustments" { | |
fileName = (file?.URLByDeletingLastPathComponent?.URLByDeletingLastPathComponent?.lastPathComponent ?? "IMG") + "." + (file?.pathExtension ?? ".jpg") | |
} else { | |
fileName = file?.lastPathComponent ?? "IMG.jpg" | |
} | |
let imagePath = (NSFileManager.defaultManager().cacheFolder.stringByAppendingPathComponent(fileName)).stringByUniqueFileName | |
let imageURL = NSURL(fileURLWithPath: imagePath) | |
let type = photoUTI ?? "public.jpeg" | |
if let photoData = photoData, imageSource = CGImageSourceCreateWithData(photoData as CFData, nil), imageDestination = CGImageDestinationCreateWithURL(NSURL(fileURLWithPath: imagePath), type, 1, nil) { | |
let cfImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) | |
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, cfImageProperties) | |
CGImageDestinationFinalize(imageDestination) | |
let finalDest = self.provider.currentPath.stringByAppendingPathComponent(fileName) | |
self.provider.copyLocalFileToPath(imageURL, toPath: 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.stringByAppendingPathComponent(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.stringByAppendingPathComponent("IMG.jpg").stringByUniqueFileName | |
jpeg.writeToFile(imagePath, atomically: true) | |
} | |
} | |
gcd.async(.Main) { | |
self.reloadTable() | |
} | |
}, failureBlock: { (error) -> Void in | |
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { | |
let imagePath = self.path.stringByAppendingPathComponent("IMG").stringByUniqueFileName | |
let jpeg = UIImageJPEGRepresentation(image, 1.0) | |
jpeg?.writeToFile(imagePath, atomically: true) | |
} | |
}) | |
*/ | |
} | |
picker.dismissViewControllerAnimated(true, completion: nil) | |
} | |
func imagePickerControllerDidCancel(picker: UIImagePickerController) { | |
picker.dismissViewControllerAnimated(true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment