2015-11-19 71 views
1

我資料圖片,也可作爲一個按鈕,當人點擊畫面中的圖像選擇顯示了,他們可以選擇他們想要爲他們的個人資料圖片使用的圖片,但imagePickerController不會被調用,並解析數據庫圖像文件不會被調用。爲什麼會發生這種情況,我該如何解決?imagePickerController代表不叫,斯威夫特

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) 

    } 
    } 
} 
+0

檢查您是否在類中添加了'UIImagePickerControllerDelegate' –

+0

是的,它被添加。 – coolmac

+0

// imagePicker.delegate =自 爲什麼評論? –

回答

4
swift 2.0

及以後,如果你使用任何委託例如UIImagePickerControllerDelegate,你需要強制執行的委託方法,否則不使用或不添加在您的視圖控制器,這是什麼概念,現在按照這個 一些示例

第1步

在您的視圖控制器添加UIImagePickerControllerDelegate, UINavigationControllerDelegate

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

@IBOutlet var imageView: UIImageView! 

let imagePicker = UIImagePickerController() 

步驟2

override func viewDidLoad() { 
super.viewDidLoad() 

imagePicker.delegate = self // Its Manotary 
} 

步驟3

// Present the Image picker controller 

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

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

步驟4

// 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) 
} 

步驟-5-

// finally dismiss the Imagepicker controller 

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