Inside my application I give the user access to UIImagePickerController allowing them to either take a photo or choose from the library to save to a path in the DocumentDirectory:
//Obtaining Saving Path
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let imagePath = documentDirectory.stringByAppendingPathComponent("Photo.png")
//casting the image selected as a UIImage
var chosenImage: UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
//writing it to the set imagePath
UIImagePNGRepresentation(chosenImage).writeToFile(imagePath, atomically: true)
My aim is to access this file that has been saved from within a WebView in another ViewController, and have run some JS to have the file uploaded automatically when the page finishes loading, opposed to clicking the "Choose File" button and selecting the file etc.
This is the element I want to adjust the value of.
<input type="file" name="field_153" id="field_153">
and this is what what I am running when the web view finishes loading:
func webViewDidFinishLoad(webView: UIWebView) {
//Retreive a UIImage object from disk
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let imagePath = documentDirectory.stringByAppendingPathComponent("Photo.png")
let savedPhoto: UIImage = UIImage(contentsOfFile: imagePath)!
....
let uploadPictureJS = "var inputFields = document.getElementById('field_153'); inputFields.value = '\(imagePath)';"
self.webView.stringByEvaluatingJavaScriptFromString(uploadPictureJS) }
I've tried using both imagePath and savedPhoto, but this evidently isn't as simple as inputting a text value to the element.
Is this possible to do using JS? Or is there an easier alternative way to accomplish this?
Thanks