iOS, Swift, UIImagePicker allows editing crop tool removes transparency. Is there fix? - ios

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
}
}
}

Related

image picker controller not able to change the default crop square size after clicking image from iphone

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

How can I crop an image before loading it to the view controller

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)
}
}

UIImageView not showing picture from imagePickerController

I've looked at a lot of solutions which, sadly, did not work for me. I used this code to create an imagePickerController:
#IBAction func takePhoto(sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, _didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
dismissViewControllerAnimated(true, completion: nil)
}
But still the image I take on the camera doesn't show up in the imageView. I also imported the correct delegates. When I try to print the imageView.image to the console it returns nil. What am I doing wrong?
Please try this code. Hope it will help you.
func cameraPhoto(){
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.cameraCaptureMode = .Photo
imagePicker.cameraDevice = .Front;
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
}
Thanks

Swift - UIImage rotated 90 degrees when picked from UIImagePickerController Camera

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
})
}

UIImageView won't display chosen image from UIImagePicker

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]

Resources