ios - 获取 func imagePickerController(picker : UIImagePickerController, didFinishPackingWithMediaInfo: [String : AnyObject]) 的返回值

标签 ios swift uiimagepickercontroller

我正在尝试访问 imagePickerController 的 NSURL 返回值,但我不知道要为参数输入什么。是否有可能从这个方法获取 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()
...

}

关于ios - 获取 func imagePickerController(picker : UIImagePickerController, didFinishPackingWithMediaInfo: [String : AnyObject]) 的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875547/

相关文章:

ios - UICollectionView NSIndexPath?

iphone - 在 iPhone 应用程序中从磁盘加载图像很慢

ios - ld : warning: Could not find auto-linked library 'swiftObjectiveC'

使用相机胶卷照片前的 IOS 图像预览

ios - UIImagePickerController 中的图像版本错误

ios - UINavigationViewController - 每个后续 VC 上的右键不同吗?

html - 实现 Scrollview 委托(delegate)方法时,WKWebview Scroll 变得缓慢和滞后

ios - swift- get exc_bad_instruction (code=exc_i386_invop, subcode=0x0) 在 while 循环中替换数组值

ios - 为 UIImagePicker 设置委托(delegate)返回错误

IOS UITableView右半截屏