2016-06-27 50 views
0

我使用的UIImagePickerController與此代碼的UIImagePickerController畫廊代表並沒有叫

func openCamera() 
{ 
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) 
    { 

     picker.allowsEditing = false 
     picker.sourceType = UIImagePickerControllerSourceType.Camera 
     picker.cameraCaptureMode = .Photo 
     picker.modalPresentationStyle = .FullScreen 
     presentViewController(picker, 
           animated: true, 
           completion: nil) 

    } 
    else 
    { 
     openGallary() 
    } 
} 
func openGallary() 
{  
    let menuViewController = UIImagePickerController() 

    menuViewController.modalPresentationStyle = .Popover 
    menuViewController.preferredContentSize = CGSizeMake(320, 320) 
    //menuViewController.tableView = FrontTable 

    let popoverMenuViewController = menuViewController.popoverPresentationController 
    popoverMenuViewController?.permittedArrowDirections = .Any 
    popoverMenuViewController?.delegate = self 
    popoverMenuViewController?.sourceView = flagBtn 
    popoverMenuViewController?.sourceRect = CGRect(
     x: 15, 
     y: 25, 
     width: 1, 
     height: 1) 
    presentViewController(
     menuViewController, 
     animated: true, 
     completion: nil) 

} 


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
{ 
    picker .dismissViewControllerAnimated(true, completion: nil) 
    let image=info[UIImagePickerControllerOriginalImage] as? UIImage 

    flagBtn.setImage(image, forState: .Normal) 
    flagBtn.setImage(image, forState: .Highlighted) 
    flagBtn.setImage(image, forState: .Selected) 
} 

隨着相機委託工作正常,但與畫廊的代表不工作。對於相機,我已經在類中定義了「picker」變量,並在「Viewdidload」中定義了它的代表self。我也嘗試在openGallary函數中使用picker變量,但它也不起作用。

回答

0

我覺得這兩條線可能會有所幫助:

menuViewController.delegate = self 
menuViewController.sourceType = .PhotoLibrary 

確保您已經添加UINavigationControllerDelegateUIImagePickerControllerDelegateViewController

+0

感謝它的工作 – Avim