ios app coding: 當圖像從gallery UIImagePickerController 選擇後, 獲取data並上傳到Firebase storage的問題.

firebase ios storage upload photo from gallery issue有問題,
官方範例也有問題.
像這樣:從UIImagePickerController獲得NSURL,在模擬器上ok,但在iPhone上 error。

error message:
2016-06-25 12:45:23.695 Swiftexample[4187:21957121] Body file is unreachable:/var/mobile/Media/DCI1M/100APPLE/IMG_0022.JPG
Error Domain=NSCocoaErrorDomain Code=257"The file"IMG_0022.JPG" couldn’t be opened because you don’t have permission to view it."

官方範例也有錯,google要加油了...
這樣不知有多少開發者遇到問題不知怎辦.

solution:把原function 替換.


guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
        
        let assets = PHAsset.fetchAssets(withALAssetURLs: [referenceUrl], options: nil)
        let asset = assets.firstObject
        asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
            let imageFile = contentEditingInput?.fullSizeImageURL
            let filePath = Auth.auth().currentUser!.uid + "/\(imageFile!.lastPathComponent)"
        
        //--
        let imageData = UIImagePNGRepresentation(image)!
        let photoStorageRef = Storage.storage().reference().child(filePath)
        
        let metadata = StorageMetadata()
        metadata.contentType = "image/png"
        
        photoStorageRef.putData(imageData, metadata: metadata) { metadata, error in
            if let error = error {               
                self.urlTextView.text = "Upload Failed 1: " + error.localizedDescription;               
                print("Error uploading:\(error.localizedDescription)")
                return
            } else {         
                self.uploadSuccess(photoStorageRef, storagePath: (metadata?.path)!)
            }
}           
        

})




[update 2019/9/14]
error: StorageExampleSwift[430:25460] Failed to issue sandbox extension for file file:///var/mobile/Media/DCIM/102APPLE/IMG_2044.JPG, errno = 1


solution:

// if it's a photo from the library, not an image from the camera
if #available(iOS 8.0, *), let referenceUrl = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.referenceURL)] as? URL {
let assets = PHAsset.fetchAssets(withALAssetURLs: [referenceUrl], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL
let imageData_new = contentEditingInput?.displaySizeImage

guard let imageData = imageData_new!.jpegData(compressionQuality: 0.8) else { return }

let imagePath = Auth.auth().currentUser!.uid +
"/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(imageFile!.lastPathComponent.lowercased())"

let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"


// [START uploadimage]
let storageRef = self.storage.reference(withPath: imagePath)
storageRef.putData(imageData, metadata: metadata) { (metadata, error) in

if let error = error {
print("Error uploading: \(error)")
self.urlTextView.text = "Upload Failed 1: \(error)"
return
}

self.uploadSuccess(storageRef, storagePath: imagePath)
}





// [END uploadimage]

})

}



留言

這個網誌中的熱門文章

最爛的銀行服務-玉山銀行

Mark App Design Apps - Terms and Privacy Policy (服務條款,隱私權政策)

SMR疊瓦式hdd致命缺陷被解決????!!!

ios app 上架時app icon要注意事項

更改google drive預設存放目錄位置Change Google Drive Default Folder Location in Windows

google play 正式發布前測試報告...非常好用.

舊有app在Android 12 閃退問題& app Splash screens

app bundle and bundletool. 關於aab安裝問題

關於google play console app應用程式簽署

Google Play badge徽章產生器