0

我在使用UIImagePickerController時試圖上傳圖片時,一直在收到這個錯誤/錯誤。我正在按照本教程進行操作raywenderlich.com (link)從iPhone訪問UIImage時出錯,但不是從Xcode模擬器中訪問時出錯

我還設置了iOS 10隱私設置。

這是錯誤代碼,當我在我的物理iPhone上運行應用程序時,我只在Xcode sim上運行該應用程序,該應用程序工作正常並且完美地上傳照片。

[MC] Reading from public effective user settings. 
[Generic] Creating an image format with an unknown type is an error 
[14875:2893865] Body file is unreachable: /var/mobile/Media/DCIM/122APPLE/IMG_2468.JPG 
    Error Domain=NSCocoaErrorDomain Code=257 "The file 「IMG_2468.JPG」 couldn’t   
be opened because you don’t have permission to view it." UserInfo= 
{NSURL=file:///var/mobile/Media/DCIM/122APPLE/IMG_2468.JPG, 
NSFilePath=/var/mobile/Media/DCIM/122APPLE/IMG_2468.JPG, 
NSUnderlyingError=0x174642040 {Error Domain=NSPOSIXErrorDomain Code=1 
"Operation not permitted"}} 
Error uploading photo: An unknown error occurred, please check the server response. 

這是我使用從photoLibrary獲取圖像的代碼

extension KalamViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
func imagePickerController(_ picker: UIImagePickerController, 
          didFinishPickingMediaWithInfo info: [String : Any]) { 

    picker.dismiss(animated: true, completion:nil) 

    // 1 
    if let photoReferenceUrl = info[UIImagePickerControllerReferenceURL] as? URL { 
     // Handle picking a Photo from the Photo Library 
     // 2 
     let assets = PHAsset.fetchAssets(withALAssetURLs: [photoReferenceUrl], options: nil) 
     let asset = assets.firstObject 

     // 3 
     if let key = sendPhotoMessage() { 
      // 4 
      asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in 
       let imageFileURL = contentEditingInput?.fullSizeImageURL 

       // 5 
       let path = "\(FIRAuth.auth()?.currentUser?.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReferenceUrl.lastPathComponent)" 

       // 6 
       self.storageRef.child(path).putFile(imageFileURL!, metadata: nil) { (metadata, error) in 
        if let error = error { 
         print("Error uploading photo: \(error.localizedDescription)") 
         return 
        } 
        // 7 
        self.setImageURL(self.storageRef.child((metadata?.path)!).description, forPhotoMessageWithKey: key) 
       } 
      }) 
     } 
    } else { 
     // Handle picking a Photo from the Camera - TODO 
    } 
} 

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    picker.dismiss(animated: true, completion:nil) 
} 

}

回答

0

我不認爲我曾經有過成功從這樣的文件系統上傳,所以我只是在內存中執行它:

// pragma mark - UIImagePickerDelegate overrides 
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    // Get local file URLs 
    guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return } 
    let imageData = UIImagePNGRepresentation(image)! 

    // Get a reference to the location where we'll store our photos 
    let photosRef = storage.reference().child("chat_photos") 

    // Get a reference to store the file at chat_photos/<FILENAME> 
    let photoRef = photosRef.child("\(NSUUID().UUIDString).png") 

    // Upload file to Firebase Storage 
    let metadata = FIRStorageMetadata() 
    metadata.contentType = "image/png" 
    photoRef.putData(imageData, metadata: metadata).observeStatus(.Success) { (snapshot) in 
    // Upload successful! 
    } 

    // Clean up picker 
    dismissViewControllerAnimated(true, completion: nil) 
}