gpt4 book ai didi

ios - imagePickerController 委托(delegate)未调用,Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:21:11 24 4
gpt4 key购买 nike

我有一个个人资料图片,它也充当一个按钮,当人们点击图片时,图像选择器会出现,他们可以选择他们想要用作个人资料图片的图片,但是 imagePickerController 未被调用,并且 Parse db 图像文件未被调用。为什么会发生这种情况,我该如何解决?

class ProfileView: UIViewController,  UIImagePickerControllerDelegate {
func userchange(sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
// imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)

}
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

let user = PFUser.currentUser()
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 0.05)
let imageFile = PFFile(name:"image.jpg", data:imageData!)
user!["ProPic"] = imageFile;
imageFile.save()
//NOTHING IS BEING PRINTED
print("called")
user!.saveInBackgroundWithBlock(nil)


self.dismissViewControllerAnimated(true, completion: nil)

}
}
}

最佳答案

swift 2.0 及以后的版本中,如果您使用任何委托(delegate),例如 UIImagePickerControllerDelegate,您需要强制实现委托(delegate)方法,否则不要在您的 View Controller 中使用或添加,这是概念,现在遵循这个一些例子

第一步

在您的 View Controller 中添加 UIImagePickerControllerDelegate、UINavigationControllerDelegate

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var imageView: UIImageView!

let imagePicker = UIImagePickerController()

第二步

  override func viewDidLoad() {
super.viewDidLoad()

imagePicker.delegate = self // Its Manotary
}

第三步

 // Present the Image picker controller

@IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary

presentViewController(imagePicker, animated: true, completion: nil)
}

第四步

// call the delegate method for pick the image

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}

dismissViewControllerAnimated(true, completion: nil)
}

第五步

// finally dismiss the Imagepicker controller

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}

关于ios - imagePickerController 委托(delegate)未调用,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com