What\'s currently working in my code:
I select a JPG or PNG from the Photo Library (using standard ImagePicker methods), and convert that image to NSData using:
Swift 4 update:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
let assetPath = info[UIImagePickerControllerReferenceURL] as! URL
self.dismiss(animated: true, completion: nil)
switch mediaType {
case kUTTypeImage, kUTTypeLivePhoto, kUTTypeGIF:
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if (assetPath.absoluteString.hasSuffix("GIF")) || (assetPath.absoluteString.hasSuffix("gif")){
print("GIF")
let options = PHImageRequestOptions()
options.isSynchronous = true
options.isNetworkAccessAllowed = false
options.deliveryMode = .highQualityFormat
guard let asset = PHAsset.fetchAssets(withALAssetURLs: [assetPath], options: nil).lastObject else { return }
PHImageManager.default().requestImageData(for: asset, options: options) { data, uti, orientation, info in
guard let info = info else { return }
if let error = info[PHImageErrorKey] as? Error {
print("Cannot fetch data for GIF image: \(error)")
return
}
if let isInCould = info[PHImageResultIsInCloudKey] as? Bool, isInCould {
print("Cannot fetch data from cloud. Option for network access not set.")
return
}
if let gifImageData = data {
// do something with data (it is a Data object)
}
}
} else {
// this is the basic image
}
}
case kUTTypeMovie:
if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
// using video type
}
default:
print("Others")
}
}