2016-11-30 28 views
0

私の写真ライブラリ(最近撮影した写真をFirebasicにアップロードしたもの)に持っていこうとしています。ストレージ。swift:フォトライブラリから画像を撮ってファイアベースにアップロードする

以下のコードは、画像の保存で、ライブラリを、それを追加:

// MARK: - Save image 

@IBAction func savePhoto(_ sender: Any) { 
    UIImageWriteToSavedPhotosAlbum(licensePhoto.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 
} 


//MARK: - Add image to Library 

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { 
    if let error = error { 
     // we got back an error! 
     let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert) 
     alert.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(alert, animated: true) 
    } else { 
     let alert = UIAlertController(title: "Saved!", message: "Image saved successfully", preferredStyle: .alert) 
     alert.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(alert, animated: true) 
    } 
} 

任意の助けをいただければ幸いです。 致命的なエラー:

私は私だけの誤差を与えることを、次のコードを試してみました!:

// . . . upload license photo to firebase 


    var data = NSData() 
    data = UIImageJPEGRepresentation(licensePhoto.image!, 0.8)! as NSData 
    // set upload path 
    let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")" 
    let metaData = FIRStorageMetadata() 
    metaData.contentType = "image/jpg" 
    DataService.Instance.storageRef.child(filePath).put(data as Data, metadata: metaData){(metaData,error) in 
     if let error = error { 
      print(error.localizedDescription) 
      return 
     }else{ 
      //store downloadURL 
      let downloadURL = metaData!.downloadURL()!.absoluteString 
      //store downloadURL at database 
      DataService.Instance.dbRef.child("users").child(FIRAuth.auth()!.currentUser!.uid).updateChildValues(["userPhoto": downloadURL]) 
     } 

    } 
} 
+0

コードを画像としてではなくテキストとして入力してください。それ以外の場合は、コピーや変更が非常に難しくなります。 – EmilioPelaez

+0

申し訳ありません、私はそれを編集します。ありがとう – LizG

答えて

0

が、私は答えを見つけ申し込み後のオプションの値をアンラップしながら、予想外にnilが見つかりました

// . . . upload license photo to firebase 

    let storage = FIRStorage.storage().reference() 
    let tempImgRef = storage.child("tmpDir/tmpImage.jpg") 

    var data: NSData = NSData() 
    data = UIImageJPEGRepresentation(licensePhoto.image!, 0.8)! as NSData 

    let metaData = FIRStorageMetadata() 
    metaData.contentType = "image/jpeg" 

    tempImgRef.put(data as Data, metadata: metaData, completion: { (metaData, error) in 
     if error == nil { 
      print("success") 

     } else { 
      print(error?.localizedDescription as Any) 
     } 
    }) 
} 
関連する問題