如何从 swift 中的图像选择器获取本地照片 url 4

How to get photo local url from image picker in swift 4

我想通过 pod Alamofire (SessionManager) 将相册 (iOS) 中的照片上传到服务器(代码中通过 .getWalluploadUrl 获取的 uploadUrl 是正确的) 我正在尝试使用 imagePicker 选择照片。但是接下来在Alamofire上传中出现错误:

multipartEncodingFailed(reason: Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:///var/mobile/Containers/Data/Application/366A5FD0-D597-44DC-A6C7-943DDEAB03B7/Documents/asset.JPG, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “asset.JPG” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/366A5FD0-D597-44DC-A6C7-943DDEAB03B7/Documents/asset.JPG, NSFilePath=/var/mobile/Containers/Data/Application/366A5FD0-D597-44DC-A6C7-943DDEAB03B7/Documents/asset.JPG, NSUnderlyingError=0x28375c9f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}))

为什么这个文件不存在?我在哪里使用 imagePicker 获取错误的本地路径?

代码(我知道这段代码很烂,但我只是想弄清楚本地路径的问题):

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let imageUrl          = info[UIImagePickerController.InfoKey.referenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageName!)
    let image             = info[UIImagePickerController.InfoKey.originalImage]as! UIImage
    let data              = image.pngData()

    self.getWalluploadUrl { (uploadUrl) in
        let sessionManager = SessionManager.default
        sessionManager.upload(
            multipartFormData: {
                data in
                data.append(
                    localPath!, //????? Incorrect path
                    withName: "file",
                    fileName: imageName!,
                    mimeType: "image/jpeg"
                )
        },
            to: uploadUrl,
            encodingCompletion: {
                (encodingResult) in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload
                        .uploadProgress { (progress) in
                            print("progress:",progress)
                        }
                        .responseJSON { response in
                            switch response.result {
                            case .success(let value):
                                let json = JSON(value)
                                print(json)
                            case .failure(let error):
                                print(error)
                            }

                    }
                case .failure(let error):
                    print(error)
                }
        }
        )

    }

我找到了 Swift 4.2 的正确方法。有效:

let photoLocalUrl = (info[UIImagePickerController.InfoKey.imageURL] as? URL)!

不需要使用.path.lastPathComponent. 这已经是完整正确的本地路径(如URL).

注意:所有照片 URL(以及其他文件)都是临时照片,下次启动应用程序时会有所不同。