I'd like to let the user chose a background for the main view of the app.
I'd like to know how I can do that for another view, even if the app is killed.
I thought about saving the path of the image in Userdefaults and then get it from the main view, but I'm not able to set the background from an asset URL. Is there a way to do this ? Or a better way maybe ?
Here is what I've done so far:
#IBOutlet weak var imageView: UIImageView!
var imagePicker = UIImagePickerController()
#IBAction func background(_ sender: Any) {
self.imagePicker.allowsEditing = false
self.imagePicker.sourceType = .savedPhotosAlbum
imagePicker.modalPresentationStyle = .overCurrentContext
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.imageView.contentMode = .scaleAspectFit
let imageUrl = info[UIImagePickerControllerReferenceURL] as! URL
UserDefaults.standard.set(imageUrl, forKey: "background")
imageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.imagePicker = UIImagePickerController()
dismiss(animated: true, completion: nil)
}
How about loading URL from defaults in viewDidLoad() and then call setImageFromURL? Of course you have to set the image to your ImageView
func setImageFromURl(stringImageUrl url: String){
if let url = NSURL(string: url) {
if let data = NSData(contentsOf: url as URL) {
self.image = UIImage(data: data as Data)
}
}
}
Related
I am trying to select the images each with 1 button but it not displaying any images which I had selected.
var imagePicker = UIImagePickerController()
#IBAction func chooseImage1(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker.delegate = self
imagePicker.sourceType = .SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
#IBAction func chooseImage2(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Button capture")
imagePicker2.delegate = self
imagePicker2.sourceType = .SavedPhotosAlbum
imagePicker2.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage1.image = pickedImage
let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
chooseImage2.image = pickedImage2
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
I would like to be able to select two individual different photos on the imageView each. Thank you for the help.
UIImagePickerController can only pick images one at a time. To do a multiple selection, you should use a well updated API like this.
I have the following problem. I use a imagepickerdelegate to choose a image from my photo library and to apply this in the UIImageView. The problem is that it won't update the UIImageview with my chosen image.
I inherit both
UIImagePickerControllerDelegate, UINavigationControllerDelegate
This is my image view.
let profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 99 / 2
imageView.layer.masksToBounds = true
imageView.image = UIImage(named: "profileimg")
imageView.contentMode = .scaleToFill
return imageView
}()
Then the functions to handle choosing and setting the image inside the UIImageView look like these.
func addProfilePhoto() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedPickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedPickedImage
} else if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = pickedImage
}
if let selectedImage = selectedImageFromPicker {
print("set image")
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
self.profileImageView.image = selectedImage
self.dismiss(animated: true, completion: nil)
}
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
My log gives the following messages when I tap on the UIImageView:
[Generic] Creating an image format with an unknown type is an error
And when I choose an image it confirms that there is an image but it won't show.
set image
Any idea what I am doing wrong?
I checked your code, seem you have not code for display profileImageView on screen. So, you do not see it. Example:
profileImageView.frame = ........
self.view.addSubview(profileImageView)
I have a profile picture which also acts as a button, when the person clicks the picture the image selector shows up and they can choose the picture they want to use as their profile picture, but the imagePickerController is not being called, and the Parse db image file is not being called. Why is this happening, how can I fix it?
class ProfileView: UIViewController, UIImagePickerControllerDelegate {
func userchange(sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
// imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let user = PFUser.currentUser()
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 0.05)
let imageFile = PFFile(name:"image.jpg", data:imageData!)
user!["ProPic"] = imageFile;
imageFile.save()
//NOTHING IS BEING PRINTED
print("called")
user!.saveInBackgroundWithBlock(nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
in swift 2.0 and onwards if you using any delegates for example UIImagePickerControllerDelegate , you need to compulsory implement the delegate methods , else dont use or dont add in your viewcontroller, this is the concept , now follow this
some example
Step-1
add UIImagePickerControllerDelegate, UINavigationControllerDelegate in your view Controller
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
Step-2
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self // Its Manotary
}
Step-3
// Present the Image picker controller
#IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
Step-4
// call the delegate method for pick the image
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
Step-5
// finally dismiss the Imagepicker controller
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
I'm working on an onboarding flow for my app. I'm wanting to allow users to pick an avatar, display that avatar in an image view, and then select a background photo and display that in another image view. Currently, I have everything coded up, but for some reason when I present the picker controller, it defaults to controller for the avatar. I want the avatar button to only control the avatar button background image and the background button to only control the header image background image. Here's my code:
#IBAction func getBackground(sender: AnyObject) {
let backgroundPickerController = UIImagePickerController()
backgroundPickerController.delegate = self
backgroundPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
backgroundPickerController.allowsEditing = true
self.presentViewController(backgroundPickerController, animated: true, completion: nil)
}
#IBAction func selectAvatar(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.addAvatar.setImage(image, forState: .Normal)
// Dismiss controller
self.dismissViewControllerAnimated(false, completion: nil)
}
func backgroundPickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.headerImage.image = image
self.dismissViewControllerAnimated(false, completion: nil)
}
The delegate function imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) also returns the UIImagePickerController that finished picking an image.
So you can use an if statement to check if the picker that finished is pickerOne or pickerTwo. Then you implement different behaviour according to the result of that.
Maybe set the pickers to nil after they have finished to clean up some memory.
class multiPickerVC : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var pickerOne : UIImagePickerController?
var pickerTwo : UIImagePickerController?
override func viewDidLoad() {
//
}
#IBAction func getBackground(sender: AnyObject) {
pickerTwo = UIImagePickerController()
pickerTwo!.delegate = self
pickerTwo!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerTwo!.allowsEditing = true
self.presentViewController(pickerTwo!, animated: true, completion: nil)
}
#IBAction func selectAvatar(sender: AnyObject) {
pickerOne = UIImagePickerController()
pickerOne!.delegate = self
pickerOne!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerOne!.allowsEditing = true
self.presentViewController(pickerOne!, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
if picker == pickerOne {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.addAvatar.setImage(image, forState: .Normal)
} else if picker == pickerTwo {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.headerImage.image = image
}
self.dismissViewControllerAnimated(true, completion: nil)
}
}
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]