2016-08-13 11 views
1

私は、User ImageとしてURLを取得する機能を作成しています。しかし、NSUUIDによって作成された私のアップロードイメージ名機能。したがって、私は各ユーザープロファイルの画像の名前がわからないでしょう。どのようにしてimg名をハードコードするのではなく、すべてのユーザーに対してユーザーのイメージを取得できるようにコードを改善できますか?Swift Firebase Storage未知の名前(NSUUID)でイメージを取得する方法

func getUserProfilePic(){ 
let uid = FIRAuth.auth()?.currentUser?.uid 
let profilePath = refStorage.child("\(uid)").child("profilePic/xxxxx.jpg") // xxxxx = NSUUID 

profilePath.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    if (error != nil) { 
    print("got no pic") 
    } else { 

    let profilePic: UIImage! = UIImage(data: data!) 
    self.imageV.image = profilePic 
    print("got pic") 
    } 
} 
} 

パスは、UID/profilePic /ある - これについて移動する二つの方法があることができ

func uploadingPhoto(){ 
let uid = FIRAuth.auth()?.currentUser?.uid 
let imgName = NSUUID().UUIDString + ".jpg" 
let filePath = refStorage.child("\(uid!)/profilePic/\(imgName)") 
var imageData = NSData() 
imageData = UIImageJPEGRepresentation(profilePic.image!, 0.8)! 

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

let uploadTask = filePath.putData(imageData, metadata: metaData){(metaData,error) in 
    if let error = error { 
    print(error.localizedDescription) 
    return 
}else{ 
    let downloadURL = metaData!.downloadURL()!.absoluteString 
    let uid = FIRAuth.auth()?.currentUser?.uid 
    let userRef = self.dataRef.child("user").child(uid!) 
    userRef.updateChildValues(["photoUrl": downloadURL]) 
    print("alter the new profile pic and upload success") 

    } 

} 
+1

アップロード画像の機能コード... –

+0

@AaronSaunders更新 –

答えて

1

アップロード機能< -file名 - - >: -

1. Firebaseデータベースにユーザprofile_pictureのFirebaseストレージパスを保存し、プロファイル画像のダウンロードを開始するたびに取得します。

2.)CoreDataでユーザーがプロフィール画像をアップロードするたびにファイルのパスを保存し、ユーザーのprofile_picという保存された場所にFILE_PATHを得るためにpathするたびにヒット。パスを保存

: -

func uploadSuccess(metadata : FIRStorageMetadata , storagePath : String) 
{ 
    print("upload succeded!") 
    print(storagePath) 

    NSUserDefaults.standardUserDefaults().setObject(storagePath, forKey: "storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)") 
    //Setting storagePath : (file path of your profile pic in firebase) to a unique key for every user "storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)" 
    NSUserDefaults.standardUserDefaults().synchronize() 

} 

、あなたのイメージのダウンロードを開始するたびに、あなたのパスを取得: -

let storagePathForProfilePic = NSUserDefaults.standardUserDefaults().objectForKey("storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)") as? String 

注: - 私はcurrentUser IDを使用しています、あなたはユーザー固有を使用することができますが複数のユーザーのプロファイル写真をダウンロードする場合は、uidを入力するだけです。

+0

ご質問ありがとうございます。NSUserDefaultにすべてのユーザーの詳細を保存することをお勧めしますか? –

+1

バックエンドで検索する必要があるファイルパスだけでなく、残りはあなた次第です... – Dravidian

3

Firebase StorageとFirebase Realtime Databaseを一緒に使用して、そのUUID - > URLマッピングを保存し、ファイルを "リスト"することを強くお勧めします。 Realtime Database will handle offline use casesにはCore Dataも同様です。したがって、Core DataまたはNSUserDefaultsを実際に使用する理由はありません。いくつかのこれらの作品がどのように相互作用するかを示すためのコードの下で:

共有:

// Firebase services 
var database: FIRDatabase! 
var storage: FIRStorage! 
... 
// Initialize Database, Auth, Storage 
database = FIRDatabase.database() 
storage = FIRStorage.storage() 

アップロード:

let fileData = NSData() // get data... 
let storageRef = storage.reference().child("myFiles/myFile") 
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in 
    // When the image has successfully uploaded, we get it's download URL 
    let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString 
    // Write the download URL to the Realtime Database 
    let dbRef = database.reference().child("myFiles/myFile") 
    dbRef.setValue(downloadURL) 
} 

ダウンロード:詳細については

let dbRef = database.reference().child("myFiles") 
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in 
    // Get download URL from snapshot 
    let downloadURL = snapshot.value() as! String 
    // Create a storage reference from the URL 
    let storageRef = storage.referenceFromURL(downloadURL) 
    // Download the data, assuming a max size of 1MB (you can change this as necessary) 
    storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    // Do something with downloaded data... 
    }) 
}) 

Zero to App: Develop with Firebaseを参照してください、とはこれを行う方法の具体的な例はassociated source codeです。

関連する問題