Camera is not responding - ios

Would please review my code, I do not have any errors, but when I open my view controller, the camera doesn't start there is only a black screen, even the buttons do not appear.
import UIKit
import AVFoundation
class CameraViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
let picker = UIImagePickerController()
#IBOutlet weak var myImageView: UIImageView!
#IBOutlet weak var cameraView: UIView!
#IBAction func photoLibrary(_ sender: UIBarButtonItem) {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
picker.modalPresentationStyle = .popover
present(picker, animated: true, completion: nil)
picker.popoverPresentationController?.barButtonItem = sender
}
#IBAction func shootPhoto(_ sender: UIBarButtonItem) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
//MARK: - Delegates
private func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
myImageView.contentMode = .scaleAspectFit //3
myImageView.image = chosenImage //4
dismiss(animated:true, completion: nil) //5
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
I only get completely black screen, even no hints from Swift. I do not know what to do now.

This happens when you have not set an initial viewcontroller. Please make sure the viewcontroller you want to load first is set as initial viewcontroller in your storyboard.

Related

How to CROP Images in Swift?

Hello i am using UIImagePicker to take images but while picking images i want to CROP that image.. How to do that
i am able to pick images from gallery to imageview but while selecting image i need to crop the image.
I have tried below code:
class AddNewEventViewController: UIViewController, UIPickerViewDelegate,UIImagePickerControllerDelegate, UIPickerViewDataSource,UINavigationControllerDelegate {
#IBOutlet weak var imgPick: UIImageView!
var picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
picker.delegate = self
picker.allowsEditing = true
}
#IBAction func addProfileBtnAction(_ sender: Any) {
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheetController.popoverPresentationController?.sourceView = self.contentView
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
}
actionSheetController.addAction(cancelAction)
let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
self.openCameraPicker()
}
actionSheetController.addAction(takePictureAction)
let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
self.openPhotoGallery()
}
actionSheetController.addAction(choosePictureAction)
//Present the
self.present(actionSheetController, animated: true, completion: nil)
}
func openCameraPicker() {
picker.sourceType = UIImagePickerController.SourceType.camera
picker.cameraCaptureMode = .photo
picker.allowsEditing = true
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
}
func openPhotoGallery() {
picker.sourceType = .photoLibrary
picker.allowsEditing = true
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
imgPick.image = img
}
else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.imgPick.image = image
}
dismiss(animated: true, completion: nil)
}
}
like this i am getting.. no crop
here i am getting images from gallery to imageview but not getting crop.
How to crop the images, please help me with the code.
I check with your code and i found you are missing to set delegate of UIImagePickerController
I try with photoLibrary for now
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.openPhotoGallery()
}
func openPhotoGallery() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = true
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
imageView.image = img
}
dismiss(animated: true, completion: nil)
}
}

Use of unresolved identifier 'image'

I have a slight problem. I'm trying to make a camera in Swift Xcode, however, I've run into one problem and that is that "ImagePicked.image = image" keeps showing error. I have no idea why it does this. Photo of the interface of the app
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var ImagePicked: UIImageView!
#IBAction func openCamera(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) { //Is the camera an available source type?
let imagePicker = UIImagePickerController() //Declare variable imagePicker
imagePicker.delegate = self
imagePicker.sourceType = .camera;
imagePicker.allowsEditing = false //Tell Image Pickr not to edit camptured photo
self.present(imagePicker, animated: true, completion: nil) //Show the photo to the user
}
func openLibrary(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //Check if device has access to photo library
let imagePicker = UIImagePickerController() //Set up variable imagePicker
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary; //Set the source type to library
imagePicker.allowsEditing = true //Allow editing, so the user can move and crop their photo
self.present(imagePicker, animated: true, completion: nil) //Show UIImagePickerController to the user
}
}
func saveImage(_ sender: Any) {
let ImageData = ImagePicked.image!.jpegData(compressionQuality: 0.6)
let compressedJPGImage = UIImage(data: ImageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
let alertController = UIAlertController(title: "Complete", message: "Your image has been saved.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default); alertController.addAction(okAction); self.present(alertController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard (info[.originalImage] as? UIImage) != nil else {
fatalError ("Expected a dictionary containtaining an image, but was provided with the following: \(info)")
ImagePicked.image = image //PROBLEM
dismiss(animated: true, completion: nil)
}
}
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
}
Update didFinishPickingMediaWithInfo as below,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.originalImage] as? UIImage else { return }
ImagePicked.image = image
picker.dismiss(animated: true, completion: nil)
}
As #rmaddy commented, you should rearrange the methods as below,
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var ImagePicked: UIImageView!
#IBAction func openCamera(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) { //Is the camera an available source type?
let imagePicker = UIImagePickerController() //Declare variable imagePicker
imagePicker.delegate = self
imagePicker.sourceType = .camera;
imagePicker.allowsEditing = false //Tell Image Pickr not to edit camptured photo
self.present(imagePicker, animated: true, completion: nil) //Show the photo to the user
}
}
func openLibrary(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //Check if device has access to photo library
let imagePicker = UIImagePickerController() //Set up variable imagePicker
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary; //Set the source type to library
imagePicker.allowsEditing = true //Allow editing, so the user can move and crop their photo
self.present(imagePicker, animated: true, completion: nil) //Show UIImagePickerController to the user
}
}
func saveImage(_ sender: Any) {
let ImageData = ImagePicked.image!.jpegData(compressionQuality: 0.6)
let compressedJPGImage = UIImage(data: ImageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
let alertController = UIAlertController(title: "Complete", message: "Your image has been saved.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default); alertController.addAction(okAction); self.present(alertController, animated: true, completion: nil)
}
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Accessing and using IOS camera

Im trying to learn how to access and use the camera on IOS devices, trufully i cant find all that much info on it, at least not thats up to date or useful to learn at a beginner level.
Regardless, I've tried the below code but nothing happens when I click the button to open the camera it just freezes and does nothing, am i missing something or have i done something wrong?
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var myImg: UIImageView!
var newMedia: Bool!
#IBAction func cameraRoll(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
newMedia = false
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismiss(animated: true, completion: nil)
if mediaType.isEqual(to: kUTTypeImage as String){
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
myImg.image = image
if (newMedia == true) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishsavingWithError:contextInfo:)), nil)
} else if mediaType.isEqual(to: kUTTypeMovie as String){
//vid support
}
}
}
func image(image: UIImage, didFinishsavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer){
if error != nil {
let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
newMedia = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Type 'UIImagePickerControllerSourceType' has no member 'photoLibrary'

This is the code:
// Only allow photos to be picked,not taken.
imagePickerController.sourceType = .photoLibrary
this is the error: Type 'UIImagePickerControllerSourceType' has no member 'photoLibrary'
Just trying to complete the official tutorial Start Developing iOS Apps (Swift)
//MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
//Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagepickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked,not taken.
imagePickerController.sourceType = .photoLibrary
//Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, competion: nil)
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
Okay, it looks like you are probably usingSwift 2.2 (maybe it's this for Swift 2.3 also). You want this syntax:
imagePickerController.sourceType = .PhotoLibrary
Note the capitalization. Keep in mind, Xcode 8.2 (released 12 December 2016) will be the last version of Xcode to support Swift 2.x. Sometime in 2017 you will probably need to use Swift 3 for new App Store submissions.
EDIT:
Here's my full Swift 2.2 code, followed by my Swift 3.0 code. Please note two things:
I'm also using the Camera app and have checks to make sure it's present - which isn't the case for the simulator.
Your app will crash in iOS 10 unless you add the following to your info.plist file:
NSCameraUsageDescription
Used to capture new image for photo effect
NSPhotoLibraryUsageDescription
Used to select an image for photo effect
You may put different values in the tags.
Swift 2.2:
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
// picker.modalPresentationStyle = .Popover
presentViewController(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegueWithIdentifier("ShowEditView", sender: self)
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destinationViewController as? EditViewController {
vc.image = image
}
}
}
}
Swift 3.0 (only the extension code has syntax changes):
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
// picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
// vc.image = images[0]
}
}
}

More Image from Library

I'm writing a simple app and I'm not able to show more images from Photo Library in two separate ImageView.
Can anyone help me?
Here's my code:
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var myImageView: UIImageView!
#IBOutlet weak var myImageVie2: UIImageView!
let picker = UIImagePickerController()
func noCamera(){
let alertVC = UIAlertController(title: "No Camera",message: "Sorry, this device has no camera",preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK",style:.Default,handler: nil)
alertVC.addAction(okAction)
presentViewController(alertVC,animated: true,completion: nil)
}
#IBAction func photoFromLibrary(sender: UIBarButtonItem) {
picker.allowsEditing = false //2
picker.sourceType = .PhotoLibrary //3
picker.modalPresentationStyle = .Popover
presentViewController(picker,animated: true,completion: nil)//4
picker.popoverPresentationController?.barButtonItem = sender
}
//take a picture, check if we have a camera first.
#IBAction func shootPhoto(sender: UIBarButtonItem) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,animated: true,completion: nil)
} else {
noCamera()
}
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject])
{
var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
myImageView.contentMode = .ScaleAspectFit
myImageView.image = chosenImage
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true,completion: nil)}
}

Resources