Force square image from UIImagePickerController - ios

I think I have a similar problem like this other question's title suggests, but the accepted answer is not at all what I need.
I want to "force" the user to use a square image. Currently, when choosing a horizontally "longer" image (landscape image), one could theoretically make a selection with black bars top and bottom which results in a selected image that is NOT squared.
Here my code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selected: UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage {
selected = img
selected = UIImage.imgToSquare(image: selected)
doSomethingWithImage(selected!)
}
picker.dismiss(animated: true, completion: nil)
}
UIImage.imgToSquare(image: UIImage) is an Extension that I have and use as a temporary fallback but it would be much more elegant if the "maximum pinch/zoom-out" gives a square image.
To whoever marked this as a duplicate.. In which way is this a duplicate? I could not find a solution on this on the whole internet, and what you linked was not related to UIImagePickerController....
I already said I'm able to crop an image to a square already with my Extension (UIImage.imgToSquare(image: UIImage))....
But what I want, is the image SELECTION to always be square.
I'll attach a screenshot.
What I have:
What I want:
(the second one - what I want - would still be scrollable horizontally)

I think this will helpful for you..
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
var myimage:UIImage!
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView.contentMode = .ScaleAspectFit
myImageView.image = pickedImage
myimage = pickedImage
}
// So the user can pick only Square Image not others
if myimage.size.height == myimage.size.width
{
dismissViewControllerAnimated(true, completion: nil)
}
}

Related

how to create a pdf from image which taken by camera with pdfkit

Hi I am new to pdfkit and iOS applications, I want to take a picture with device and then convert the image to pdf. taking pictures with camera and saving it on Camera Roll works completely fine, the problem is when I want to create pdf from that image it needs the name of the image, how I can find it?
and another question is it possible to implement any pdf editor with pdfkit to edit image?
Appreciate any help. below is my imagePickerController codes.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
if (Mylibrary)
{
}
else
{
UIImageWriteToSavedPhotosAlbum(pickedImage!, nil, nil, nil);
let image = UIImage(named: "????") //the problem is here
let newPage = PDFPage(image: image!)
}
picker.dismiss(animated: true, completion: nil)
}
There's no need to instantiate a new image since the image is already stored in the pickedImage variable. You just need to initialize the new PDFPage object with pickedImage. Use ImageKit to edit the image before using it to initialize a new PDFPage object.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
// Adds pickedImage to the user’s Camera Roll album.
UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)
// Creates a new PDFPage object and initializes it with pickedImage.
let newPage = PDFPage(image: pickedImage)
dismiss(animated: true, completion: nil)
}

IOS Swift load (local)image with URL

Can we load images found locally on the device with SDWebimage using URL option? if yes then Im trying to use SDWebimage to display images, below is my code and i am unable to load image in the imageview. its still blank. kindly help me to understand what im doing wrong?
func addImage(sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
picController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
picController.allowsEditing = true
self.presentViewController(picController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
titleImageView.sd_setImageWithURL(url)
self.dismissViewControllerAnimated(true, completion: nil)
}
Note that the UIImagePickerController delegate
imagePickerController: didFinishPickingMediaWithInfo
will have the info as described in documentation-
A dictionary containing the original image and the edited image, if an
image was picked; or a filesystem URL for the movie, if a movie was
picked. The dictionary also contains any relevant editing information.
The keys for this dictionary are listed in Editing Information Keys.
so if it is image that you are looking at, it will be right there in the dictionary itself.
You can get original image directly-
let image = info[UIImagePickerControllerOriginalImage]
if let image = image {
//Set this image to your image view directly
titleImageView.image = image
}
Here info as a NSDictonary and get that key is UIImagePickerControllerOriginalImage
see this code :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
imagePreview.image = tempImage
self.dismissModalViewControllerAnimated(true)
}

Selecting image from library doesn't call "didFinishPickingImage" function - Swift

I have a UIImagePickerController with the option to select from library.
When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary.
func accessLibrary(sender: UIButton!) {
NSLog("access library")
choseFromLibrary = true
self.libraryPicker = UIImagePickerController()
self.libraryPicker!.allowsEditing = false
self.libraryPicker!.sourceType = .PhotoLibrary
self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}
This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
var image = info[UIImagePickerControllerOriginalImage] as? UIImage
if (image != nil) {
if (choseFromLibrary) {
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
else {
if (frontFacing) {
image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
}
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
}
}
The above function works if the image is taken using the camera, but not using the library.
When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.
I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.
didFinishPickingImageWithInfo is a delegate message (UIImagePickerControllerDelegate). The problem, however, is that you have not given your image picker any delegate! Thus, it has no one to send delegate messages to.
if you want to implement a Instagram like image picker than try this
Image Picker like Instagram
The delegate function is updated in new swift versions from 4 to upwards.
Use this function instead
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { }

How can I return a string of a photo filename?

I am using either the camera or the library app to take/select an image
eg.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// User has selected an image
// To dismiss the image picker
self.dismissViewControllerAnimated(true, completion: nil)
// Select the Image of interest
// retrieve the object via key from documentation
self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
If I set a breakpoint at self.imageView.image and step down, I can get the image properties but I don't know how to get the filename.

UIImagePickerController AllowsEditing not working

I am using UIImagePickerController to allow the user to take a picture. I want to allow him/her to edit it afterwards but, whatever I do, I get nothing.
Here is my code (I am using Xamarin):
UIImagePickerController imagePicker = new UIImagePickerController ();
// set our source to the camera
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
// set what media types
//imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.Camera);
// show the camera controls
imagePicker.ModalPresentationStyle = UIModalPresentationStyle.CurrentContext;
imagePicker.ShowsCameraControls = true;
imagePicker.AllowsEditing = true;
imagePicker.SetEditing (true,true);
imagePicker.PreferredContentSize = new SizeF(900,600);
imagePicker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
imagePicker.Title="taste.eat. image";
// attach the delegate
imagePicker.Delegate = new ImagePickerDelegate();
// show the picker
NavigationController.PresentViewController(imagePicker, true,null);
Am I missing something?
EDIT:
I have followed the tutorial and I am getting to the screen with the rectangle, but if i pan or zoom it just snaps back to the center once I lift my finger. Is it possible to get to this screen from the photos application?
When using UIImagePickerController's delegate
method - imagePickerController:didFinishPickingMediaWithInfo:, we get the image using
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
This code will always return the original image, even if editing is ON.
Try using
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
This will return the edited image if editing is ON.
Hope this helps.
The AllowsEditing property simply allows the user to crop to a square if picking an image and trim the video if picking a video.
Any other functionality needs to be implemented with custom UI and code.
See this question:iPhone SDK - How to customize the crop rect in UIImagePickerController with allowsEditing on?
What you are showing in the screenshot is not part of UIImagePickerController, unfortunately
SWIFT 3
I was having a hard time returning the cropped image (simple mistake on my end). Instead of using UIImagePickerControllerOriginalImage, you need UIImagePickerControllerEditedImage. See below:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary contains multiple representations of the image, and this uses the cropped image.
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage
// Set yourImageView to display the selected image.
yourImage.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
SWIFT 4+
There have been some changes after Swift 4. UIImagePickerControllerEditedImage changed to UIImagePickerController.InfoKey.editedImage.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
imageView.image = selectedImage
dismiss(animated: true, completion: nil)
}
It will be work like this way.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage
userPhoto.image = selectedImage
dismiss(animated: true, completion: nil)
}
There is no way to enable filters by just changing property like allowsEditing = YES. It will only display a cropping tool. As per your screenshot it's look like you have integrated some buggy open source library and without looking at the source code it would be difficult to fix your center cropping bug.
Better to post some concrete detail about your implementation or switch to standard open source library.

Resources