Reputation: 552
I have an app that allows users to take selfies and upload it to Facebook, but the selfieTapped
button I created is not working. It brings up a black screen. It does not let the user pick a photo from their library nor does it allow the user to take a picture.
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)
}
}
Upvotes: 0
Views: 214
Reputation: 534966
This may not be the cause of the trouble, but it certainly will be trouble later: you've put one function inside of another:
@IBAction func selfieTapped(sender: AnyObject) {
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
Don't do that. They both need to be at the same level, as methods of your class. Otherwise, didFinishPickingImage
can never be called.
Upvotes: 1
Reputation: 14857
Please check if you have closed Camera
& Photos
permission for your app in Setting
.
Upvotes: 1