我会尽快介绍。用户上载个人资料照片的时间大约为10%,该应用由于内存泄漏而崩溃。我尝试了很多不同的方法来尝试防止这种情况,但是由于某种原因,这种情况发生的时间很少。请我不知道为什么某些用户在上传照片时会崩溃。 (我认为它发生在didFinishPickingMediaWithInfo函数中)。
class CustomImagePickerController: UIImagePickerController {
var imageBttn: UIButton?
}
@objc private func handleSelectPhoto() {
let alert = UIAlertController(title: "Access your photos", message: "Can Crusht open your photos so you can select a profile picture?", preferredStyle: .alert)
let action = UIAlertAction(title: "Yes", style: .default){(UIAlertAction) in
let imagePicker = CustomImagePickerController()
imagePicker.delegate = self
imagePicker.imageBttn = self.selectPhotoButton
self.present(imagePicker, animated: true)
}
let cancel = UIAlertAction(title: "No", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
return
}
extension EnterPhotoController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
weak var selectedImage = info[.originalImage] as? UIImage
let imageButton = (picker as? CustomImagePickerController)?.imageBttn
imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
self.imageFull = true
dismiss(animated: true)
self.errorLabel.text = "Registering, hang tight..."
self.errorLabel.isHidden = false
self.selectPhotoButton.isEnabled = false
let filename = UUID().uuidString
let ref = Storage.storage().reference(withPath: "/images/\(filename)")
guard let imageData = selectedImage?.jpegData(compressionQuality: 0.75) else { return }
ref.putData(imageData, metadata: nil) { (nil, err) in
guard err == nil else { return }
ref.downloadURL { (url, err) in
guard err == nil else { return }
let imageUrl = url?.absoluteString ?? ""
if imageUrl == "" {
print("fuck me man")
}
self.saveInfoToFirestore(imageUrl: imageUrl)
}
}
}
}
private func saveInfoToFirestore(imageUrl: String){
let uid = Auth.auth().currentUser?.uid ?? ""
let docData: [String: Any] = ["ImageUrl1": imageUrl]
Firestore.firestore().collection("users").document(uid).setData(docData, merge: true) { (err) in
guard err == nil else { return }
let customtabController = CustomTabBarController()
customtabController.modalPresentationStyle = .fullScreen
self.present(customtabController, animated: true)
}
}
您需要将图像调整为较小的尺寸。众所周知,ios设备捕获的照片很大,将其作为数据发送到服务器也是很大的工作量。您可以根据自己的喜好调整图像大小,而不会损失大部分图像质量。
检查此内容以调整大小。https://stackoverflow.com/a/31314494/11138557
希望这会有所帮助:谢谢:)