My imageView doesn't display the picture I choose with the UIImagePicker.
Here's all the code for it:
#IBAction func addImage(_ sender: Any) {
if !didShowCamera {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
} else {
imagePicker.sourceType = .photoLibrary
}
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
print(image)
self.jobImage.image = takenImage
self.takenImage = image
didShowCamera = true
self.dismiss(animated: true, completion: nil)
}
Any ideas how to solve it?
Use dispatch main queue
DispatchQueue.main.async {
self.jobImage.image = takenImage
self.takenImage = image
}
I guess the takenImage is nil while assigning to image view, you need to assign image to takenImage before setting to the image view. please update the method like,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
print(image)
self.takenImage = image
self.jobImage.image = takenImage
didShowCamera = true
self.dismiss(animated: true, completion: nil)
}
Replace
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
with
let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
And check whether you have add privacy-photo library access in info.plist.
See detailed at [https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1][1]
Related
image showing the imagepickercontroller
As shown in this figure, that square position is not changing or resizing when i take picture from camera but it is working from gallery. Please help to how to change the position of crop square to go to required part of image.
the code used is given below
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
print(image.size)
let croppedImage = info[UIImagePickerControllerEditedImage] as? UIImage
print(croppedImage ?? "")
let cropRect = (info[UIImagePickerControllerCropRect]! as AnyObject).cgRectValue
print(cropRect?.size ?? "")
let _ = UIImage.cropImage(image: image, toRect: CGRect.init(x: 0, y: 0, w: self.imgProfile.bounds.width, h: self.imgProfile.bounds.width))
print(image.size)
self.imgProfile.image = croppedImage
profileImgUpdated = true
}
self.dismiss(animated: true, completion: nil)
}
func openCamera(){
self.imagePicker.sourceType = .camera
self.imagePicker.allowsEditing = true
self.imagePicker.delegate = self
self.present(imagePicker, animated: true)
}
func openPhotoLibrary() {
self.imagePicker.sourceType = .photoLibrary
self.imagePicker.allowsEditing = true
self.imagePicker.delegate = self
self.present(imagePicker, animated: true)
}
In Swift 4.2 didFinishPickingMediaWithInfo info: [String : Any] is depricated, try with,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.imgProfile.image = editedImage
} else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.imgProfile.image = image
}
self.dismiss(animated: true, completion: nil)
}
To enable editing,
let picker = UIImagePickerController()
picker.allowsEditing = true
For my iOS app I need a Fotoeditor. Therefore I set:
imagePicker.allowsEditing = true
Now I would like to change the size of the editable area.
Here is the editable area, but I need the size of the usable image different:
How can I change the size of this area to use a different size for the usable image area?
Here is my code for my PickerController
#IBOutlet weak var preview: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func selectImage(_ sender: Any) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
preview.contentMode = .scaleAspectFit
preview.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
I was trying to save the edited image from the library or the camera and I didn't find anything on the web so I try this code and it works! Swift 4.2
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
imageProfile.contentMode = .scaleAspectFit
imageProfile.image = pickedImage
}
this lines of code have to be inside the function
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){}
It's impossible via UIImagePickerController, you can use this library to implement this feature (GKImagePicker).
use UIImagePickerControllerEditedImage this code
for example :-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage{
preview.contentMode = .scaleAspectFit
preview.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
Check Image height and width :-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else {return}
imageView.image = image
dismiss(animated: true, completion: nil)
print(image.size.height)
print(image.size.width)
print(image.size)
}
In my view controller, my UIImageView is set to 370x370, when I load an image it currently stretches to fit the ImageView.
This is what I currently have for picking an image:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Runs when user is picking image from library
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.previewImageView.image = pickedImage
self.selectImageButton.isEnabled = false
self.selectImageButton.isHidden = true
uploadImage(image: pickedImage)
picker.dismiss(animated: true, completion: nil)
}
}
I'm fairly new to Swift, what would be the best was to implement an image crop into my code?
When you call the ImagePicker function, set allowsEditing as true:
self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .camera
self.present(self.imagePicker, animated: true, completion: nil)
Another option, if you want a bit more customization, you can use this library: https://github.com/TimOliver/TOCropViewController
And in didFinishPickingMediaWithInfo, you do something like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
dismiss(animated: true, completion: nil)
let cropVC = TOCropViewController(image: pickedImage)
cropVC.delegate = self
cropVC.aspectRatioPickerButtonHidden = true
cropVC.aspectRatioPreset = .presetSquare
cropVC.aspectRatioLockEnabled = true
cropVC.resetAspectRatioEnabled = false
self.present(cropVC, animated: true, completion: nil)
}
}
When I get PNG image with transparent background from UIImagePicker it inserts the image with the transparent background just fine as below:
when I set:
imagePicker.allowsEditing = true
it allows me to crop as expected in the view provided by UIImagePicker
but then when I press 'Choose' it returns the image with black instead of a transparent background as below:
The only change between the two results is setting
imagePicker.allowsEditing = true
and cropping the image in the app.
Is there a way to keep transparency or at least change the black to white when cropping?
I have searched the internet and no mention of this that I can see.
Code:
func showImagePicker() {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
compImageView?.contentMode = .scaleAspectFit
compImageView?.backgroundColor = UIColor.clear
compImageView?.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
By default UIImagePickerController removes the alpha channels in the edited image. To get that transparency you have to edit image yourself. Implement delegate method like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let cropRect = info[UIImagePickerControllerCropRect] as? CGRect {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageRef: CGImage = image.cgImage!.cropping(to: cropRect)!
let image1: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
imageView.image = image1
picker.dismiss(animated: true, completion: nil)
}
}
}
Please try this code.
import MobileCoreServices //importing for mediaType kUTTypeImage
func showImagePicker() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
let mediaTypes: Array<AnyObject> = [kUTTypeImage]
imagePicker.mediaTypes = mediaTypes as! [String]
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker.sourceType == UIImagePickerControllerSourceType.photoLibrary {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
dismiss(animated: true, completion: nil)
compImageView?.image = image
}
}
}
I am using UIImagePickerController to pick images from both Camera and Library. When i get the image from the library the picture is OK. However, when i get the picture from the camera its rotating 90 degrees to the right. My app works just in portrait mode. Here is the code where i get the image
#IBAction func camPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func importPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
my Function importPhoto works fine but the other one doesn't.
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}
Any ideas?
You can try like :
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
**selectedPhoto.withHorizontallyFlippedOrientation()**
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}