在我的应用程序中,我授予用户访问UIImagePickerController的权限,允许他们拍照或从库中选择保存到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)
我的目的是访问从另一个ViewController中的WebView中保存的文件,并运行一些JS以在页面加载时自动上传文件,而不是单击“选择文件”按钮并选择文件等。
这是我要调整的值的元素。
<input type="file" name="field_153" id="field_153">
这就是我在Web视图加载完成时运行的内容:
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) }
我尝试过同时使用imagePath和savedPhoto,但这显然不如向元素输入文本值那么简单。
这可以用JS吗?或者有更简单的替代方法来实现这一目标吗?
由于