获取 func imagePickerController 的 return 值(选择器:UIImagePickerController,didFinishPackingWithMediaInfo:[String:AnyObject])

Get return value of func imagePickerController(picker: UIImagePickerController, didFinishPackingWithMediaInfo: [String : AnyObject])

我正在尝试访问 ImagePickerController 的 NSURL return 值,但我不知道要为参数输入什么。是否有可能从此方法获取 NSURL,如果不能,我还应该如何从 imagePickerControlle 获取 NSURL?,因为我已经搜索了几天。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) -> NSURL {

    let theURL: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)

    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    profilePicture.image = selectedImage

    print("searchthis\(theURL)")

    dismiss(animated: true, completion: nil)
    return theURL
}

我想在此处访问 "theURL":

@IBAction func signUpPressed(_ sender: AnyObject) {

    let authInfo = AuthInfo()

    authInfo.email = emailField.text!
    authInfo.firstName = firstField.text!
    authInfo.lastName = lastField.text!
    authInfo.username = usernameField.text!
    authInfo.password = passwordField.text!
    authInfo.profilePicture = profilePicture.image

    let url = imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: ).theURL
    let container = CKContainer.default()
    let privateData = container.privateCloudDatabase

    let record = CKRecord(recordType: "Authentication")
    record.setValue(authInfo.email, forKey: "email")
    record.setValue(authInfo.username, forKey: "username")
    record.setValue(authInfo.firstName, forKey: "firstName")
    record.setValue(authInfo.lastName, forKey: "lastName")
    record.setValue(authInfo.password, forKey: "password")
    record.setValue(authInfo.profilePicture, forKey: "profilePicture")
    record.setValue(CKAsset(fileURL: ))
    privateData.save(record, completionHandler: { record, error in
        if error != nil {
            print(error)
        }

    })

}

谢谢!

您必须单独获取个人资料图像,并且必须将 URL 和图像保存在全局变量中。 喜欢:

//first register your view controller for image picker delegates

@IBAction func profilePickBtnPressed(_ sender: AnyObject) {
    let _mediaPicker = UIImagePickerController()
    _mediaPicker.delegate = self
    _mediaPicker.mediaTypes = ["public.image" as String]
    _mediaPicker.allowsEditing = false
    _mediaPicker.sourceType = .Camera
    self.presentViewController(_mediaPicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//BTW, this is delegate method of picker controller
globalURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)

globalImage = info[UIImagePickerControllerOriginalImage] as! UIImage
profilePicture.image = selectedImage

print("searchthis\(theURL)")

dismiss(animated: true, completion: nil)
}

然后在此函数中使用这些全局保存的值:

@IBAction func signUpPressed(_ sender: AnyObject) {

let authInfo = AuthInfo()

...
authInfo.profilePicture = globalImage

let url = globalURL
let container = CKContainer.default()
...

}