【问题标题】:How to get photo local url from image picker in swift 4如何在 swift 4 中从图像选择器获取照片本地 url
【发布时间】:2019-01-25 16:34:52
【问题描述】:

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

multipartEncodingFailed(原因:Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL:file:///var/mobile/Containers/Data/Application/366A5FD0-D597-44DC-A6C7-943DDEAB03B7/Documents/asset.JPG,错误:错误域=NSCocoaErrorDomain 代码=260“无法打开文件“asset.JPG”,因为没有这样的文件。” 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 {错误域= NSPOSIXErrorDomain Code=2 "没有这样的文件或目录"}}))

为什么这个文件不存在?我在哪里使用 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)
                }
        }
        )

    }

【问题讨论】:

    标签: ios swift alamofire uiimagepickercontroller


    【解决方案1】:

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

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

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

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

    【讨论】:

    • P. S. 2:我使用 SwiftyJson(从它获取的 URL 类型)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多