I have a button to take an image . I wish to pass the image taken from camera to another view controller.The code for first viewcontroller is shown below.
#IBAction func takePhoto(sender: AnyObject)
{ let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true,completion : nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
TakenImage = info[UIImagePickerControllerOriginalImage] as? UIImage ; dismissViewControllerAnimated(true, completion: nil )
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("NoteDetailViewController") as! NoteDetailViewController
controller.takinPhoto = true
if (note != "")
{
controller.content = note
}
controller.imageFromCamera = TakenImage
if (self.tags != "")
{
controller.tagsTextField.text = self.tags
}
self.presentViewController(controller, animated: true, completion: nil)
}
#IBAction func save(sender: AnyObject)
{
UIImageWriteToSavedPhotosAlbum(TakenImage!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>)
{
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else
{
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
The code for my second viewcontroller is shown below
if (takinPhoto == true)
{
if (imageFromCamera != nil)
{
if let image1 = self.imageFromCamera
{
self.imageView2.image = image1
}
}
if (self.content != "")
{
self.contentTextField2.text = content
}
}
But image from camera is not appearing in the second viewcontroller.How can I solve this??
Updated answer.
I rewrote your code and was able to get it working locally. The main change was to use a different delegate method which is specific to photos.
#IBAction func takePhoto(sender: AnyObject) { let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true,completion : nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
takenImage = image
dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func save(sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(takenImage!, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else {
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "saveTakenImage") {
let controller = segue.destinationViewController as! NoteDetailViewController
controller.takinPhoto = true
if (note != "") {
controller.content = note
}
controller.imageFromCamera = takenImage
if (self.tags != "") {
controller.tagsTextField.text = self.tags
}
}
}
Related
I want to pick two different images on two imageView's on one ViewController, display them, and after pushing the button save picked images to firebase database and storage to its particular user. My code only able to upload one picked image, not two different, I understand that the problem with UIImagePickerController part, but how can I resolve it. Full code of viewController is below. Please help!!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectProfileImageView(sender:)))
profilePhoto.addGestureRecognizer(tapGesture)
profilePhoto.isUserInteractionEnabled = true
let wallTapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectWallpaperImageView(sender:)))
wallpaperPhoto.addGestureRecognizer(wallTapGesture)
wallpaperPhoto.isUserInteractionEnabled = true
profilePhoto.layer.cornerRadius = 60
profilePhoto.clipsToBounds = true
}
weak var activeImageView:UIImageView? = nil
#objc func handleSelectWallpaperImageView(sender: UIGestureRecognizer){
guard let sendingImageView = sender.view as? UIImageView else {
print("Ooops, received this gesture not from an ImageView")
return
}
activeImageView = sendingImageView
let actionSheet = UIAlertController(title: "New Photo", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Камера", style: .default, handler: { action in
self.showCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Альбом ", style: .default, handler: {action in
self.showAlbum()
}))
actionSheet.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
#objc func handleSelectProfileImageView(sender: UIGestureRecognizer){
guard let sendingImageView = sender.view as? UIImageView else {
print("Ooops, received this gesture not from an ImageView")
return
}
activeImageView = sendingImageView
let actionSheet = UIAlertController(title: "New Photo", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Камера", style: .default, handler: { action in
self.showCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Альбом ", style: .default, handler: {action in
self.showAlbum()
}))
actionSheet.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func showCamera() {
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
present(cameraPicker, animated: true, completion: nil)
}
func showAlbum(){
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .photoLibrary
present(cameraPicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
// selectedImage = image
activeImageView?.image = image
// currentImage = image
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
///для того чтобы загруженные фото, отображались на ProfileViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "profileVC" {
let destination = segue.destination as! ProfileViewController
destination.wImage = activeImageView?.image
}
}
#IBAction func saveTapped(_ sender: Any) {
let db = Firestore.firestore()
let did = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://crut-6c67c.appspot.com").child("profile_Image").child(did)
if let pImage = self.activeImageView?.image, let imageData = pImage.jpegData(compressionQuality: 0.1) {
storageRef.putData(imageData, metadata: nil, completion: {(metadata, Error) in
if Error != nil, metadata != nil {
return
}
storageRef.downloadURL { (url: URL?,error: Error?) in
if let profileImageUrl = url?.absoluteString{
db.collection("suppliers").document("ip").collection("ipinfo").document(did).setData(["Profile Image":profileImageUrl], merge: true)
}
}
})
}
let storyBoard: UIStoryboard = UIStoryboard(name: "Profile", bundle: nil)
let profileViewController = storyBoard.instantiateViewController(identifier:profile.Storyboard.profileViewController) as? ProfileViewController
self.view.window?.rootViewController = profileViewController
self.view.window?.makeKeyAndVisible()
}
}
You can make an instance variable in your viewController i.e.
private var isProfilePhotoSelecting = true
When user tap on profileImageView in handleSelectProfileImageView method set isProfilePhotoSelecting to true i.e.
#objc func handleSelectProfileImageView(sender: UIGestureRecognizer){
guard let sendingImageView = sender.view as? UIImageView else {
print("Ooops, received this gesture not from an ImageView")
return
}
// Updated the image under consideration
isProfilePhotoSelecting = true
let actionSheet = UIAlertController(title: "New Photo", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Камера", style: .default, handler: { action in
self.showCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Альбом ", style: .default, handler: {action in
self.showAlbum()
}))
actionSheet.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
And on wallpaperImageView tapping in handleSelectWallpaperImageView method set isProfilePhotoSelecting to false i.e.
#objc func handleSelectWallpaperImageView(sender: UIGestureRecognizer){
guard let sendingImageView = sender.view as? UIImageView else {
print("Ooops, received this gesture not from an ImageView")
return
}
// Updated the image under consideration
isProfilePhotoSelecting = false
let actionSheet = UIAlertController(title: "New Photo", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Камера", style: .default, handler: { action in
self.showCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Альбом ", style: .default, handler: {action in
self.showAlbum()
}))
actionSheet.addAction(UIAlertAction(title: "Отмена", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
Then update your imagePickerDelegate to :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
if isProfilePhotoSelecting {
profilePhoto.image = image
} else {
wallpaperPhoto.image = image
}
}
dismiss(animated: true, completion: nil)
}
This will help you set both your images to their respective imageView. Then in saveTapped(_:) method you can check both imageViews for images and upload them i.e. Your saveTapped() method should look like this
#IBAction func saveTapped(_ sender: Any) {
let db = Firestore.firestore()
let did = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://crut-6c67c.appspot.com")
if let profileImage = self.profilePhoto.image, let imageData = profileImage.jpegData(compressionQuality: 0.1) {
let profileStorage = storageRef.child("profile_Image").child(did)
profileStorage.putData(imageData, metadata: nil, completion: {(metadata, Error) in
if Error != nil, metadata != nil {
return
}
profileStorage.downloadURL { (url: URL?,error: Error?) in
if let profileImageUrl = url?.absoluteString {
db.collection("suppliers").document("ip").collection("ipinfo").document(did).setData(["Profile Image":profileImageUrl], merge: true)
}
}
})
}
if let wallpaperImage = self.wallpaperPhoto.image, let imageData = wallpaperImage.jpegData(compressionQuality: 0.1) {
let wallpaperStorage = storageRef.child("wallpaper_Image").child(did)
wallpaperStorage.putData(imageData, metadata: nil, completion: {(metadata, Error) in
if Error != nil, metadata != nil {
return
}
wallpaperStorage.downloadURL { (url: URL?,error: Error?) in
if let wallpaperImageUrl = url?.absoluteString {
// Do your stuff with wallpaper image url here
}
}
})
}
let storyBoard: UIStoryboard = UIStoryboard(name: "Profile", bundle: nil)
let profileViewController = storyBoard.instantiateViewController(identifier:profile.Storyboard.profileViewController) as? ProfileViewController
self.view.window?.rootViewController = profileViewController
self.view.window?.makeKeyAndVisible()
}
Note: This will not wait for the images to upload as you are waiting for them to upload. But it depends on your usecase if you want to wait until the image uploading completes and then move to ProfileViewController, you can use DispatchGroup for that purpose.
I hope your saving the last choosen image on activeImageView and passing that to Firebase, hence only one image is getting uploaded.
Instead create and Array and both the images choosen from handleSelectWallpaperImageView and handleSelectWallpaperImageView, then loop through the Array and send to Firebase.
Check this for more information: https://stackoverflow.com/a/49934285/1244403
Created option Boolean variable
var isProfilePhotoSelecting:Bool?
#objc func handleSelectWallpaperImageView(sender: UIGestureRecognizer){
isProfilePhotoSelecting = false
///
other code
}
#objc func handleSelectProfileImageView(sender: UIGestureRecognizer){
isProfilePhotoSelecting = true
///
other code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
if isProfilePhotoSelecting == true {
profilePhoto.image = image
}else {
wallpaperPhoto.image = image
}
}
}
In the saveTapped need to be added storage references to each chosen image and its respective database.
#IBAction func saveTapped(_ sender: Any) {
let db = Firestore.firestore()
let did = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://crut-6c67c.appspot.com").child("profile_Image").child(did)
if let profileImage = self.profilePhoto.image, let imageData = profileImage.jpegData(compressionQuality: 0.1){
storageRef.putData(imageData, metadata: nil, completion: {(metadata, Error) in
if Error != nil, metadata != nil {
return
}
storageRef.downloadURL { (url: URL?, error: Error?) in
if let profileImageUrl = url?.absoluteString {
db.collection("suppliers").document("ip").collection("ipinfo").document(did).setData(["Profile Image":profileImageUrl], merge: true)
}
}
})
}
let wallStoreRef = Storage.storage().reference(forURL: "gs://crut-6c67c.appspot.com").child("Wallpaper_Image").child(did)
if let wallpaperImage = self.wallpaperPhoto.image, let imageData = wallpaperImage.jpegData(compressionQuality: 0.1) {
wallStoreRef.putData(imageData, metadata: nil, completion: {(metadata, Error) in
if Error != nil, metadata != nil {
return
}
wallStoreRef.downloadURL { (url: URL?,error: Error?) in
if let wallpaperImageUrl = url?.absoluteString {
// Do your stuff with wallpaper image url here
db.collection("suppliers").document("ip").collection("ipinfo").document(did).setData(["Wallpaper Image":wallpaperImageUrl], merge: true)
}
}
})
}
I'm desperately trying to add video picking option to my app. The app can take a picture and select pictures but it cannot select video file. I want the users to be able to select video from the photo library just as easily as they can select a photo. But most people I have asked seem to think it is not possible. But of course, it is not. I have used many app with this feature. Or this is not something I can do with Xcode?
}
let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
actionsheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let mypickerController = UIImagePickerController()
mypickerController.delegate = self
mypickerController.sourceType = .photoLibrary
self.present(mypickerController, animated: true, completion: nil)
}
}))
actionsheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .camera
self.present(myPickerController, animated: true, completion: nil)
}
}))
actionsheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionsheet, animated: true, completion: nil)
}
#IBAction func onSaveButton(_ sender: Any) {
print("screenshot pressed")
//let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
//appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()
if firstImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: firstImage)
}
if secondImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: secondImage)
}
if thirdImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: thirdImage)
}
let image = GlobalFunction.sharedManager.takeScreenshot(theView: backView)//takeScreenshot(theView: backView)
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}
#IBAction func onCancelButton(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(vc, animated: true, completion: nil)
}
#IBAction func onAddButton(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(vc, animated: true, completion: nil)
}
#objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo: UnsafeRawPointer) {
DispatchQueue.main.async{
self.showAlertView(message: "Your story has been saved to your Camera Roll", completionHandler: { (a) in
let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()
})
//UIAlertView(title: "Success", message: "Your story has been saved to your Camera Roll", delegate: nil, cancelButtonTitle: "Close").show()
}
}
func takeScreenshot(theView: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(theView.bounds.size, true, 0.0)
theView.drawHierarchy(in: theView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
func circleCropDidCancel() {
dismiss(animated: false, completion: nil)
}
func circleCropDidCropImage(_ image: UIImage) {
dismiss(animated: false, completion: nil)
if selected == 1{
firstImage.image = image
}else if selected == 2{
secondImage.image = image
}else if selected == 3{
thirdImage.image = image
}
}
}
UIImagePickerController has a mediaTypes property. It determines what kind of media the picker can pick. You are not setting mypickerController.mediaTypes to include videos.
I am trying to allow the user to edit an existing photo using the move and scale view and allow them to edit any picture they pick or take but I can't get the move and scale view to appear correctly. How do I get the title and circle view to show? This is what it looks like now:
and this is what I want:
func addImage(sender: UIButton) {
self.view.endEditing(true)
let alert = UIAlertController(title: "", message: nil, preferredStyle: .actionSheet)
var numberAlert = UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.default, handler: { action in
self.takePhoto()
})
alert.addAction(numberAlert)
numberAlert = UIAlertAction(title: "Choose Photo", style: UIAlertActionStyle.default, handler: { action in
self.choosePhoto()
})
alert.addAction(numberAlert)
numberAlert = UIAlertAction(title: "Edit Photo", style: UIAlertActionStyle.default, handler: { action in
self.editPhoto()
})
alert.addAction(numberAlert)
numberAlert = UIAlertAction(title: "Delete Photo", style: UIAlertActionStyle.default, handler: { action in
self.deletePhoto()
})
alert.addAction(numberAlert)
let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler:nil)
cancelAlert.setValue(UIColor.blue, forKey: "titleTextColor")
alert.addAction(cancelAlert)
self.present(alert, animated: true, completion: nil)
}
func choosePhoto() {
imagePicker = nil
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func takePhoto() {
imagePicker = nil
imagePicker = UIImagePickerController()
imagePicker.resignFirstResponder()
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func editPhoto() {
}
func deletePhoto() {
}
//MARK: - Saving Image here
#IBAction func save(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(self.contact.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
//MARK: - Add image to Library
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
NSLog("Error saving image: \(error)")
} else {
self.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.none)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
self.contact.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.none)
}
I am trying to create an controller to handle all image functionality so that you can easily bind all camera actions from any view controller.
Ideally what my goal was to create a class with a function that returns an UIImage and allow my self to write individual completion handlers
ie.
let imagePicker = ImagePickerAlertController(frame:self.view.frame,controller:self)
imagePicker.displayAlert(){
imageValue in if let image = imageValue {
myImageView.image = image
}
}
However, i cannot seem to save the image or even access the image that i have taken from the camera. The imagePickerController function does not seem to be hitting.
import UIKit
class ImagePickerAlertController: UIView, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var UIViewController : UIViewController?
let imagePicker: UIImagePickerController! = UIImagePickerController()
init(frame: CGRect, controller: UIViewController){
self.UIViewController = controller
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
self.UIViewController = nil
super.init(coder: aDecoder)
}
public func displayAlert(){
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let galleryAction = UIAlertAction(title: "Choose Photo",style:.default) {action -> Void in print("ok")}
let cameraAction = UIAlertAction(title: "Take Photo",style:.default) {action -> Void in self.takePicture() }
let cancelAction = UIAlertAction(title: "Cancel",style:.cancel) {action -> Void in }
alert.addAction(cancelAction)
alert.addAction(cameraAction)
alert.addAction(galleryAction)
self.UIViewController?.present(alert,animated:true,completion:nil)
}
private func takePicture() {
if (UIImagePickerController.isSourceTypeAvailable(.camera)){
if UIImagePickerController.availableCaptureModes(for: .rear) != nil || UIImagePickerController.availableCaptureModes(for: .front) != nil{
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
imagePicker.delegate = self
self.UIViewController?.present(imagePicker,animated: true,completion: nil)
}
else {
postAlert(title: "Rear camera doesn't exist",message:"Application cannot access the camera.")
}
}
else {
postAlert(title: "Camera inaccessable",message:"Application cannot access the camera.")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("got image")
if let pickedImage:UIImage = (info[UIImagePickerControllerOriginalImage]) as? UIImage {
let selectorToCall = Selector(("imageWasSavedSuccessfully:didFinishSavingWithError:context:"))
UIImageWriteToSavedPhotosAlbum(pickedImage, self, selectorToCall, nil)
}
imagePicker.dismiss(animated: true,completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("cancel")
self.UIViewController?.dismiss(animated: true, completion: nil)
}
func imageWasSavedSuccessfully(image: UIImage, didFinishSavingWithError error : NSError!, context: UnsafeMutablePointer<()>){
print("image saved")
if (error) != nil {
print(error)
}
else {
print("good to go")
}
}
func postAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.UIViewController?.present(alert, animated: true, completion: nil)
}
}
The problem is that you try to present imagePicker when your UIViewController already has a modal view controller presented above.
displayAlert():
self.UIViewController?.present(alert,animated:true,completion:nil)
takePicture():
self.UIViewController?.present(imagePicker,animated: true,completion:
nil)
So you should dismiss UIAlertController as soon as you don't need it:
let cameraAction = UIAlertAction(title: "Take Photo",style:.default) {action -> Void in
alert.dismiss(animated: true, completion: nil)
self.takePicture()
}
Now viewController can present without any issues
I've tried to find proper answer but no success.
I have code as below which works fine (I can get picture from library) but image size I got always is <= device width.
To be precisely - image in device gallery is for example 4k and picked image is "UIImage: 0x7ce6a5e0> size {640, 480} orientation 0 scale 1.000000" for iPhone 4s
When I'm changing "self.imagePicker.allowsEditing = true" to "false" I got no image (nil).
My goal is to pick from library original size image.
Can someone help me ?
#IBAction func newButton(sender: UIButton) {
let alert: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let SelectImage = UIAlertAction(title: NSLocalizedString("TakePhoto", comment: "comment"), style: .Default){ (action) in
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
self.imagePicker.cameraCaptureMode = .Photo
self.presentViewController(self.imagePicker, animated: true, completion: nil)
} else {
self.noCamera()
}
}
alert.addAction(SelectImage)
let chooseImage = UIAlertAction(title: NSLocalizedString("PhotoLibrary", comment: "comment"), style: .Default){ (action) in
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .PhotoLibrary
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
alert.addAction(chooseImage)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "comment"), style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let selectedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
print(selectedImage)
takenPhoto = cropToSquare(image: selectedImage)
Util.addRecord(takenPhoto)
}
dismissViewControllerAnimated(true, completion: nil)
performSegueWithIdentifier("saveToSettings", sender: self)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}