func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imgProfileIcon.image=info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
print(info)
if let _ = imagePicked
{
if imagePicked.isEqualToData(UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)!)
{
print("U Picked The Same Image")
}
else
{
print("Different Image Picked")
}
}
imagePicked=UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)
isImagePicked=true
}
This is the delegate function that I use to select an image from The photoLibrary and my objective is to ensure that I don't hit a web service if an user selects the same image..... So while I was trying to compare images it always prints "Different Image Picked" Even though I pick the same image from the photo library.
I've tried all possibilities....even with .equal() but it shows the same... I think that some additional data is added due to which the comparison fails.
Can someone help me out?
Related
I want the user to upload an image from the photos library along with more input so I can write it to my database.
I made an object with the received data from the user and I uploaded it (using ".child()") to firebase realtime database, but then I had to add the photo to the object so I used the UIImagePicker to upload the photo then I got stuck.
my problem is that i don't know what type of info I should get from the image so I can add it to the object.
I'm not sure if using an object is the right choice but since the data I’m adding is related to a specific item I thought it would be suitable.
// The object
let object: [String : Any] = ["areaname": areaName! as Any ,"spotNo": spotNo, "loactionLat": areaLat, "locationLong": areaLong]
database.child("Areas").child("Area_\(Int.random(in: 0..<100))" ).setValue(object)
#IBAction func chooseImageButton() {
print("Add image button was pressed")
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
extension AddAreaViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
print("\(info)")
if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
}
picker.dismiss(animated: true, completion: nil) }
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
Firebase structure
My solution is saving the image using FirebaseStorage. And turn the image url to string write into database. When you need loading image. You can search by that urlstring.
I think #Evan Lu is correct.
You don't need to store the image on firebase database or realtime databse.
You can store the image on FirebaseStorage and save the url on database.
I am new to swift, I am using swift 3.
I am trying to pick multiple image from photo library and I am using ELCimagepickercontroller
However when I am trying to read the images from the array I got error: Type 'Any' has no subscript members
My Code as below:
please let me know what's wrong
func elcImagePickerController(_ picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [Any]!) {
self.dismiss(animated: true, completion: nil)
var i = 0
for item in info as [AnyObject]
{
i += 1
var imageview = UIImageView(image: (info[UIImagePickerControllerOriginalImage] as? [String]))
// var name = .uiImageJPEGRepresentation()!
}
}
Since the info parameter is an array of dictionaries, you need to properly cast item in your for loop.
func elcImagePickerController(_ picker: ELCImagePickerController, didFinishPickingMediaWithInfo info: [Any]) {
self.dismiss(animated: true, completion: nil)
for item in info as [String : Any]
{
if let image = item[UIImagePickerControllerOriginalImage] as? UIImage {
var imageview = UIImageView(image: image)
}
}
}
There were several other issues in your code. Don't needlessly add !. In fact, avoid all uses of ! until you fully comprehend their proper use. Until then, each use is a potential crash.
I use Firebase as the back-end of my app. When user finish authentication they will go to profile create page. It displays the user profile picture from facebook.
I use this code to display
func displayProfilePic(user: FIRUser?){
let photoURL = user?.photoURL
struct last {
static var photoURL: NSURL? = nil
}
last.photoURL = photoURL; // to prevent earlier image overwrites later one.
if let photoURL = photoURL {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
let data = NSData.init(contentsOfURL: photoURL)
if let data = data {
let image = UIImage.init(data: data)
dispatch_async(dispatch_get_main_queue(), {
if (photoURL == last.photoURL) {
self.profilePic.image = image
}
})
}
})
} else {
profilePic.image = UIImage.init(named: "DefaultPic")
}
However, I also let user to pick their own profile picture by camera or photo library. When user choose their Image it will display the picked Image for 1 to 2 second, and display back the facebook profile picture. That's mean, the "picked image cannot replace the facebook profile picture"
It is my code for the camera and photo library
func openGallary(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
print("Pick Photo")
self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.imagePicker.allowsEditing = true
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
}
func openCamera(){
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
self.imagePicker.allowsEditing = true
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}else{
print("you got no camara")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
profilePic.image = image
}
Firebase Authentication takes a photo URL, so the public location where a profile photo for the user is present. If you want to allow the user to upload a new profile photo, you will have to find a place to store the photo and then put the URL into the Firebase Authentication profile.
One place to keep such a profile picture would be Firebase Storage. See the documentation on how to upload an image from iOS and then generate a download URL that you store in the user profile.
I am attempting to grab an NSURL from an image or video when a user selects it through a imagepickercontroller, using the Photos Framework
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url: NSURL = (info["UIImagePickerControllerReferenceURL"] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return UIImagePickerControllerReferenceURL
}
I receive the error "unexpected non void return value in void function".
The end goal is to use the NSURL to upload the image / video / gif to a Firebase Database.
I'm honestly not sure if my logic here is correct, I'm still new to coding. Any help is greatly appreciated!
In swift, you have to say in the at the end of the function before the opening "{" what the return value is going to be, if you don't, then it is considered a void function. A void function means that it does not return any value. So, to tell it that it will return a NSURL, try this:
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) -> NSURL {
let url: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return url
}
Just copy and paste this, the only difference is that it tells the function what the return value will be, and will no longer return that error. I also suggest removing the "" around the
(info[UIImagePickerControllerReferenceURL] as! NSURL)
This will return a NSURL.
I am trying to take a picture with the camera and then detect the faces in it. But it doesn't work... The results array returns a count of zero. I have tested this code with a picture of somebody from the internet and it returned 1 found face. Here is my code:
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
Idea.CurrentIdea.idea.mockups.append(PFFile(data: UIImageJPEGRepresentation(pickedImage, 0.5)!))
//Face Detection
let cid:CIDetector = CIDetector(ofType:CIDetectorTypeFace, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh]);
let cii = CIImage(CGImage: pickedImage.CGImage!)
let results:NSArray = cid.featuresInImage(cii)
print(results.count)
for r in results {
let face:CIFaceFeature = r as! CIFaceFeature;
NSLog("Face found at (%f,%f) of dimensions %fx%f", face.bounds.origin.x, face.bounds.origin.y, face.bounds.width, face.bounds.height);
}
}
dismissViewControllerAnimated(true, completion: nil)
}
Any ideas? Thanks! There isn't much on the web about it recently.
Make sure that your image orientation and the expected image orientation for the detector are the same. See this answer for more detail: https://stackoverflow.com/a/17019107/919790