Image shows up in iPhone simulator but not ipad - Swift - ios

When I go to select an image from my camera roll and display it in my view it doesn't appear on the Ipad simulator. It works great on the iphone. Here is my code
//--CONTROLS HOW TO PICK THE IMAGE WHEN THIS BUTTON IS CLICKED--\\
#IBAction func chooseImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.navigationBar.tintColor = UIColor.whiteColor()
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
//--AFTER THE IMAGED IS PICKED THIS BRINGS BACK THE VIEW--\\
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
println("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
imageToPost.image = image
photoSelected = true
}

Apparently, the UIImageView called imageToPost is not displayed correctly by the iPad simulator. Check your storyboard file and make sure the image view is correctly wired to the outlet.
Also, check any constraints that might push the image view off the screen.

Related

UIImagePickerController returns image with black background on right side when the property .allowsEditing is set to true

I'm using UIImagePickerController for picking images from gallery. Here is the code
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
isfrompicker = true
if let image = info[.editedImage] as? UIImage {
// I'm getting image with black background on right side as shown.
}
}
Link to image
I'm not sure why I'm getting black background on right side. Same code is working fine on some devices. But I'm getting issue with my iPhone X.
I got the same problem a few ago.
Then i found a solution on
Link to page
Try adding this line before presenting imagePicker
UINavigationBar.appearance().isTranslucent = true

Is it possible to set dimensions for UIImagePickerController's editor?

I'm trying to make users choose a photo from their library and crop this photo to specific dimensions like a circular , 80 x 60 sized photo.
#IBOutlet var imageView: UIImageView!
#IBAction func choosePhoto(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePickerController.allowsEditing = true
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let choosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = choosenImage
dismiss(animated: true, completion: nil)
}
I pick photos using this code but edit screen is a rectangle so it is not what i'm looking for.Is it possible to change the edit screen's dimensions ?
iOS doesn't have any circular crop feature by default. You will need to integrate external library for implementing crop feature.
You can make use of these for circular cropping https://github.com/kekearif/KACircleCropViewController https://github.com/TimOliver/TOCropViewController

How to set an image from the gallery to UIButton background? Swift 4

I have a rounded button which represent a User Avatar. By clicking on this button I invoke an ImagePicker and then trying to set picked image to UIbutton.
First I do this:
#IBAction func choseAvatar(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true, completion: nil)
}
Then this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
userAvatar.setImage(image, for: .normal)
self.dismiss(animated: true, completion: nil)
}
The image picked successfully, but there is a problem. The image is stretched down to the bottom of the screen. How to do it right?
I also tried
userAvatar.imageView?.contentMode = .scaleAspectFit
userAvatar.imageView?.image = pickedImage
And:
userAvatar.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
Any ideas?
Make sure your constraints are set correctly for the imageView. Make sure you use AspectFit (or Fill if you have a fixed width and height), and make sure you have clipToBounds enabled.

Selecting photo returns to blank UI

I am trying to make an image uploader on my ios app.
EDIT 7
This question is now more clearly asked here: Present Modal for Image Select Erases UI
END OF EDIT 7
The first thing I am trying to do is enable the user to select an image that they want for upload.
EDIT 6
I have simplified all the code to simply one button press. The Modal presentation deletes the previous page's UI. How I prevent this.
import UIKit
class AddImageToStandViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loadImageButtonTapped(sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
I have found that the selection works just fine and does not destroy the UI if I push/segue from a different to this controller.
END OF EDIT 6
Below this is the other methods that I have tried before this
So far, this code enables a user to press a button and choose a photo from their photos. But, after they click the photo it just returns them to an empty View except the navbar at the bottom.
It seems my user is being returned, not to the view that they came from, but to a separate empty view. How do I return the user to the view that they came from?
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var image: UIImageView!
...
#IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
...
let me know if there is any more information that I should give.
EDIT 1
this code has the same effect
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
I have tried adding this line below but I get an error stating that CreateStandViewController.Type cannot be converted to the expected UIViewController.
self.presentViewController(CreateStandViewController, animated: true, completion: nil)
EDIT 2
This code below doesnt error but also changes nothing
let createStandController = CreateStandViewController()
self.presentViewController(createStandController, animated: true, completion: nil)
EDIT 3
I have literally copied all the code from here: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/ and now have:
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
This has the same issue as before. When the image is selected I am sent back to a view that is white with the navbar at the bottom.
EDIT 4
I have run the same code in its own separate project as a stand alone view controller and it works. There is some code that I have in the background of my app that is killing the functionality of the code (maybe). I am guessing it may have something to do with the navbar?
EDIT 5
I removed the tab bar at the bottom by seguing to the image selecting view via a present Modally segue, but it had no effect.
Just replace your
self.dismissViewControllerAnimated(true, completion: nil)
with
picker.dismissViewControllerAnimated(true, completion: nil)
I think you will get the point after that

Camera Button Not Working

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)
}
}
Please check if you have closed Camera & Photos permission for your app in Setting.
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.

Resources