簡體   English   中英

如何從 swift 4 中的圖像選擇器獲取照片本地 url

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

我想通過 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,錯誤域=錯誤域= NSCocoaErrorDomain Code=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,NSUnderlying7Error=0x9fPOS=0x9fPOS0 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)
                }
        }
        )

    }

我為 Swift 4.2 找到了正確的方法。 有用:

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

它不需要使用.path.lastPathComponent。 這已經是完整且正確的本地路徑(作為 URL)。

注意:所有照片 URL(以及其他文件)都是臨時的,下次啟動應用程序時會有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM