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
Related
I have been using following code to select an image and display it via UIImageView:
class ViewController UIViewContoller, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad(){
super.viewDidLoad()
imagePicker.delegate = self
}
#IBAction func loadImage(_ sender: Any){
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.contentMode = .scaleAspectFit
imageView.image = pickedImage
}
dismiss(animated: true completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
}
How can I use UIImagePickerController class to select multiple images and videos and display them in a collection view?
How can I use UIImagePickerController class to select multiple images and videos
You can't. UIImagePickerController lets the user choose one image / video.
could you please help me with UIImagePickerController
I have 12 ImageViews in 1 ViewController. I need help so that each ImageView could pick different photos from Library. Someone told me to use Tags for them but I can't make it work cause i'm new in Swift.
Enable User Interaction Enabled for each UIImageView on a Storyboard.
Add TapGestureRecogniser to each UIImageView. Connect each TapGestureRecogniser with IBAction.
#IBAction func tap(_ sender: UITapGestureRecognizer) {
currentImageView = sender.view as! UIImageView
let picker = UIImagePickerController()
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
Define variable to store current UIImageView
private var currentImageView: UIImageView? = nil
Handle image selection and assign the image to currentImageView
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage]
currentImageView?.image = image as! UIImage
self.dismiss(animated: true, completion: nil)
}
It's not about the swift. You set up a "tag" number in the Interface builder (or in the code. There is a .tag property on the UIResponder descendants).
After that you make a switch in your callback method like
To make it a bit clearer:
let picker: UIImagePicker! = {
// setup your image picker here, don't forget to set the proper delegate
}()
var lastSender: AnyObject?
func onTouch(_ sender: AnyObject?) {
// add checks for the sender here
lastSender = sender
present(picker, animated: true, completion: *Your Completion*)
}
Delegate:
in
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
let image = *Get Image From Info*
lastSender?.image = image
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.
So right now I am trying to get the collection view Cells to use the photo library to update photos. Here is my code:
import UIKit
class viewCellVC: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var bookImage: UIImageView!
#IBAction func addImage(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.window?.rootViewController?.present(imagePicker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
bookImage.image = image
picker.dismiss(animated: true, completion: nil)
}
}
However, when I attempt to run the code and update the pictures, the simulator fails to open the photo library and I instead get the following error message(doesn't end the app, just fails to run the photo library):
2017-08-07 21:35:31.709183-0500 ChuBookElf[2817:66569] Warning: Attempt to present <UIImagePickerController: 0x7f907e0bf800> on <ChuBookElf.ViewController: 0x7f907de072c0> whose view is not in the window hierarchy!
Can somebody tell my what I am doing wrong? I've been on this for hours now.
For Reference, here is what the collection view cell looks like
For starters, you are creating the imagePicker in a UICollectionViewCell.
You should create the imagePicker in the ViewController that contains the said UICollectionView along with the delegate methods. Then, in didFinishPickingMediaWithInfo, set the selected image to the appropriate collectionViewCell and reload your collectionView
Have such part of my storyboard:
So I want present from UserProfileController: UIViewController "Мой профиль", which is modal, (on the top on image) UIImageViewController.
My code:
extension UserProfileController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBAction func imagePressed(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
picker.dismiss(animated: true, completion: nil)
userProfileImageView.image = image
}
}
Well, imagePickerController presented, but after I choose image I have dismissed both controllers (imagePickerController and parent - UserProfileControler).
What am I doing wrong? Thanks
P.S. May be problem is I'm showing "SideMenuNavigationController" as modal? (so advised in SideMenu framework documentation)
Try to create you UIImagePickerController reference in your class (as a variable)
// to choose an image from the Camera Roll
fileprivate lazy var imagePicker: UIImagePickerController = {
let picker: UIImagePickerController = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .savedPhotosAlbum
picker.delegate = self
return picker
}()