How to add crop functionality in camera taker app - ios

I have implemented picking image from gallery or take photo and i am displaying in in uiimage view in screen. But i want to implement crop functionality.
Measn that, after i take photo from phone then i need to crop and then i need to display in my screen. How to do that ??
Here my code :
var imagePicker: UIImagePickerController!
#IBOutlet weak var imagevView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func ChooseImage(_ sender: AnyObject) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imagevView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
Thanks

You can, to some extend, use the imagePicker.allowsEditing = true setting. If you want more advanced cropping something like what #Kamlesh suggest might be what you are looking for.

Related

How do I create a camera app that takes multiple photos swift

I am trying to create an app that takes more than a single photo. What I want is every time​ I take a photo I want the camera opens right after the first photo.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var myImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
myImage.contentMode = .scaleToFill
myImage.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
}

How to let the user upload images - how can I test this feature properly?

My code works and lets me pick a image from some kind of default saved camera roll that apple has built in on the computer. If I understand it right - whenever a person would use my app they would of course have it connect to their personal phone.
My question is: I'm trying to test my app using photos from my computer instead of the default photos that apple provides (landscapes and pictures of waterfalls). Is there a way I can directly access a folder on my desktop when I click my browse files button (btnClicked) and pull a picture out of there?
Here is what I'm using right now. How can I manipulate this to do what I need?
// Custom Image
var imagePicker = UIImagePickerController();
#IBOutlet weak var imageView: UIImageView!
// Button Clicked for Image
#IBAction func btnClicked(sender: AnyObject)
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
print("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
// View Did Load
override func viewDidLoad()
{
super.viewDidLoad();
// Do any additional setup after loading the view.
// Set the User's "Hairstyle"
userHairStyleLab.text = HairStyle;
}
// Did Receive Memory Warning
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning();
// Dispose of any resources that can be recreated.
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
imageView.image = image
}

UIView setImage:]: unrecognized selector sent to instance

import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let imagePicker = UIImagePickerController()
#IBOutlet var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
#IBAction func Select(sender: AnyObject) {
//imagePicker.sourceType =
// UIImagePickerControllerSourceType.PhotoLibrary
//imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageView.contentMode = .ScaleAspectFit
ImageView.image = image
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
I stumped on why the UIImage picked by the PickerController is not set to UIImageView. Can any one please provide insight into what I am missing here?
You have #IBOutlet var ImageView: UIImageView! in your code, but you are getting the error "UIView setImage:]: unrecognized selector". This means that you configured your view in Interface Builder as a UIView, rather than a UIImageView. You need to set up a UIImageView to be able to set the image.
Just for future reference the other reason this might happen is because you've accidentally connected the wrong UI element to your UIImageView property.
For example I dragged a NSConstraint to the UIImageView, and got the same error.

UIImageVIew.image isn't be changed

I'm newbie to iOS programming, and trying to developer "FoodTracker" in Swift tutorial. Below is all my code.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
nameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: Actions
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
//UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
//Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
//Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
//Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithinfo info: [String : AnyObject]) {
//The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
photoImageView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
//hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
}
The app is executed normally but the image can't be changed.
The normal process is click the image > select any image from Camera roll > change the image of idle screen. The problem is last step, "change the image of idle screen."
Which point is my mistake? I have compared my code with example code in tutorial again and again, but I couldn't find something helpful. Please help me.
2 things:
First declare the
let imagePicker = UIImagePickerController();
as an instance variable and then set it's delegate to self in viewdidload
override func viewDidLoad() {
super.viewDidLoad();
imagePicker.delegate=self;
}
This way it's set only once (rather reseting for each func call)
Second I suggest to use :
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
photoImageView.image=image;
}
instead of (didFinishPickingMediaWithinfo ...), which made exactly for picking image, just as you need and you don't need casting.
Here is a simplified example:
image picker example
Hope it helped
Finnaly, I figure out the solution. But it's little trivial.
The problem is Language version. I used XCode 6.4 and Swift 1.2.
But the homepage used XCode 7.0 beta and Swift 2.0.
This is the reason of my problem. And now, all works fine.
Thank you for all my responders.
Did you look at selectedImage variable? Does it nil? Try to setup breakpoint to line after setting selectedImage.

Implementing UIImagePickerController in Swift

The application runs just fine and allows me to select a photo. However, when I go to select a different photo the pickAnImage button won't work. What is the best way to allow the user to select a different image to view? Any help is appreciated.
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
#IBAction func pickAnImage(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
self.imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
try this code :
import MobileCoreServices
class SecondViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet var img:UIImageView!=nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func buttonTapped(AnyObject)
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
println("Button capture")
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
//imag.mediaTypes = [kUTTypeImage];
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
//var tempImage:UIImage = editingInfo[UIImagePickerControllerOriginalImage] as UIImage
img.image=selectedImage
self.dismissViewControllerAnimated(true, completion: nil)
}
}

Resources