Created
October 19, 2016 04:41
-
-
Save rabimba/d459913e3f100418f79b2513c5241f73 to your computer and use it in GitHub Desktop.
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 : Any]) { | |
picker.dismiss(animated: true, completion:nil) | |
guard let uid = FIRAuth.auth()?.currentUser?.uid else { return } | |
// if it's a photo from the library, not an image from the camera | |
if #available(iOS 8.0, *), let referenceURL = info[UIImagePickerControllerReferenceURL] { | |
let assets = PHAsset.fetchAssets(withALAssetURLs: [referenceURL as! URL], options: nil) | |
let asset = assets.firstObject | |
asset?.requestContentEditingInput(with: nil, completionHandler: { [weak self] (contentEditingInput, info) in | |
let imageFile = contentEditingInput?.fullSizeImageURL | |
let filePath = "\(uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\((referenceURL as AnyObject).lastPathComponent!)" | |
guard let strongSelf = self else { return } | |
strongSelf.storageRef.child(filePath) | |
.putFile(imageFile!, metadata: nil) { (metadata, error) in | |
if let error = error { | |
let nsError = error as NSError | |
print("Error uploading: \(nsError.localizedDescription)") | |
return | |
} | |
strongSelf.sendMessage(withData: [Constants.MessageFields.imageURL: strongSelf.storageRef.child((metadata?.path)!).description]) | |
} | |
}) | |
} else { | |
guard let image = info[UIImagePickerControllerOriginalImage] as! UIImage? else { return } | |
let imageData = UIImageJPEGRepresentation(image, 0.8) | |
let imagePath = "\(uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg" | |
let metadata = FIRStorageMetadata() | |
metadata.contentType = "image/jpeg" | |
self.storageRef.child(imagePath) | |
.put(imageData!, metadata: metadata) { [weak self] (metadata, error) in | |
if let error = error { | |
print("Error uploading: \(error)") | |
return | |
} | |
guard let strongSelf = self else { return } | |
strongSelf.sendMessage(withData: [Constants.MessageFields.imageURL: strongSelf.storageRef.child((metadata?.path)!).description]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment