I'm building an app that allows you to upload images from your library to the server. This server is used as essentially an image repository. For this reason, it's absolutely necessary to store it in it's original image format: Either JPG, PNG, or GIF. IE If the PNG image has transparency, that HAS to be preserved, it cannot simply be converted to a JPG.
I USED to get the image format using UIImagePickerControllerReferenceURL:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage
let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
if (assetPath.absoluteString?.hasSuffix("JPG"))! {
print("JPG")
}
else if (assetPath.absoluteString?.hasSuffix("PNG"))! {
print("PNG")
}
else if (assetPath.absoluteString?.hasSuffix("GIF"))! {
print("GIF")
}
else {
print("Unknown")
}
self.dismiss(animated: true, completion: nil)
self.showImageFieldModal(selectedImage: selectedImage)
}
But UIImagePickerControllerReferenceURL has been deprecated in iOS11. It suggests using UIImagePickerControllerPHAsset, but that's not a URL. I'm not sure what I'm supposed to do with that as a PHAsset object...
In iOS11 you can use the original image url key UIImagePickerControllerImageURL
and use URL resourceValues
method to get its typeIdentifierKey
:
if #available(iOS 11.0, *) {
if let imageURL = info[UIImagePickerControllerImageURL] as? URL {
print(imageURL.typeIdentifier ?? "unknown UTI") // this will print public.jpeg or another file UTI
}
} else {
// Fallback on earlier versions
}
You can use the typeIdentifier
extension from this answer to find out the fileURL type identifier:
extension URL {
var typeIdentifier: String? {
return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.