2015-09-29 5 views
0

У меня есть приложение, которое позволяет пользователям брать эстонцев и загружать их на Facebook, но созданная мной кнопка selfieTapped не работает. Он поднимает черный экран. Он не позволяет пользователю выбирать фотографию из своей библиотеки и не позволяет пользователю делать снимок.Кнопка камеры не работает

import UIKit 
import Social 

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { 

    @IBOutlet weak var myImageView: UIImageView! 

    @IBAction func selfieTapped(sender: AnyObject) { 

     //this method will be called when the user has taken a photo or has selected a photo from the photo library 
     func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { 

      //takes the image parameter and displays it inside myImageView 
      myImageView.image = image 

      //hides the imagePicker and animates the transition 
      self.dismissViewControllerAnimated(true, completion: nil) 

     } 

     //creates a new UIImagePickerController and sets it to the imagePicker variable 
     let imagePicker = UIImagePickerController() 

     //self the imagePicker's delegate property to the current view controller 
     imagePicker.delegate = self 

     //sets the imagePicker's sourceType property to .Camera 
     if UIImagePickerController.isSourceTypeAvailable(.Camera) { 

      //checking if a front camera is available 
      imagePicker.sourceType = .Camera 

      //if so, set it to the front camera 
      if (UIImagePickerController.isCameraDeviceAvailable(.Front)) { 

       //setting it to the front camera 
       imagePicker.cameraDevice = .Front 

      } else { 

       //setting it to the rear camera if the front is not available 
       imagePicker.cameraDevice = .Rear 
      } 

     } else { 

      //if the camera is not available, the photo library will be shown 
      imagePicker.sourceType = .PhotoLibrary 
     } 

     //presents the imagePicker modally, sliding it up from the bottom of the screen and it animates the transition 
     self.presentViewController(imagePicker, animated: true, completion: nil) 

    } 

    @IBAction func shareTapped(sender: AnyObject) { 

     //sets the serviceType property to Facebook 
     let facebookSocial = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 

     //myImageView image is added to the Facebook post 
     facebookSocial.addImage(myImageView.image) 

     //it is displayed and uses animation 
     self.presentViewController(facebookSocial, animated: true, completion: nil) 
    } 
} 

ответ

1

Пожалуйста, проверьте, если вы закрыли Camera & Photos разрешения для вашего приложения в Setting. enter image description here

+0

Это было все! Благодарю. –

1

Это не может быть причиной проблемы, но это, безусловно, будет проблем в дальнейшем: вы положили одну функцию внутри другой:

@IBAction func selfieTapped(sender: AnyObject) { 
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { 

Не делай этого. Они оба должны быть на том же уровне, что и методы вашего класса. В противном случае нельзя вызывать didFinishPickingImage.

+0

Я взял его вне функции, и он отлично работает. Я не знаю, действительно ли это проблема, но это определенно работает. –

+0

Это дало бы вам проблемы позже, после того, как вы решили проблему в вопросе! :) – matt

Смежные вопросы