簡體   English   中英

用於Tableview圖像的Firebase存儲URL

[英]Firebase storage url for tableview image

我將后端從Parse:'()遷移到Firebase,並遇到一些問題,這些問題使他們了解Firebase圍繞資產的實現並引用它們。

本質上,我需要一個詳細介紹某些文章的JSON結構,這些文章中的每一個都會有一個圖像。 當前,JSON是手動添加的,以便應用程序引用最新文章。 用於此目的的圖像將手動上傳到Firebase的存儲方面,並將其文件名添加到文章的JSON。

我的應用程序調用json,以便在表格視圖中填充文章標題標簽和同一單元格中的圖片。

我遇到的問題是要檢索存儲資產的圖像URL,以便將圖像加載到tableview中-

首先,我從數據庫中調用商品數據

  FIRDatabaseReference *ref =  [[FIRDatabase database] reference];


    [[ref child:@"articles"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        NSArray *articles = (NSArray *)snapshot.value;} withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);

    }];

從返回的快照將轉換為模型對象-將文章標題設置為模型對象以及生成的圖像URL

Article *article = [Article new];
    article.title = [articleObj valueForKey:@"title"];
    if ([articleObj objectForKey:@"image"] != [NSNull null]) {

    // Points to the root reference
    FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com"];
    // Points to "images"
    FIRStorageReference *imagesRef = [storageRef child:@"articleImages"];

    // Note that you can use variables to create child values
    FIRStorageReference *imagePath = [imagesRef child:articleObj[@"image"]];

    if (imagePath){
        article.imageURL = [NSURL URLWithString:imagePath.fullPath];

    // Fetch the download URL
    [imagePath downloadURLWithCompletion:^(NSURL *URL, NSError *error){
        if (error != nil) {
            // Handle any errors
            NSLog(@"**ERROR %@", error.description);
            NSLog(@"**ERROR %@", URL.absoluteString);
            NSLog(@"**ERROR %@",article.title);
        } else {
            article.imageURL = URL;

        }
    }];
    }

這種方法的問題(除了感覺很多代碼,它似乎應該是一個標准屬性)是文章標題立即可用,但是“ downloadURL”塊需要更長的時間,所以我要么需要觀察何時完成,並且在每篇文章完成獲取其圖像URL之前都不要重新加載我的表

因此,問題是:是否存在一種替代的非阻塞方式來獲取圖像URL,或者我在存儲門戶中看到的URL引用是否足夠可靠,可以在JSON中使用,而不是對圖像名稱的引用(因此請存儲URL在我的數據庫而不是圖像名稱中)

還供參考,我使用SDWebimage進行延遲加載圖像,但是由於上述檢索URL的延遲,當前第一次在表重新加載時URL不存在

提前致謝!

您可以通過替換三行代碼將它們轉換為單行代碼,

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com"];
// Points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"articleImages"];   
// Note that you can use variables to create child values
FIRStorageReference *imagePath = [imagesRef child:articleObj[@"image"]];

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com/articleImages/image"]];

根據您的要求,有一個SDWebImage擴展類,它僅引用FIRStorageReference並下載您的映像。

參見以下代碼:

FIRStorageReference *storageRef = [[FIRStorage storage]
                                           referenceWithPath:[NSString stringWithFormat:@"/folder/images/%@",aDictCardData[@"url"]]];

        [cell.imgDetail sd_setImageWithStorageReference:storageRef
                                       placeholderImage:nil
                                             completion:^(UIImage *image,
                                                          NSError *error,
                                                          SDImageCacheType
                                                          cacheType,
                                                          FIRStorageReference *ref) {
                                                 if (error != nil) {
                                                     NSLog(@"Error loading image: %@", error.localizedDescription);
                                                 }
                                             }];

在上面的代碼中,您可以找到帶有storageRef sd_setImageWithStorageReference方法,以輕松訪問FireBase Image

您可以從此處找到此擴展類文件: https : //github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI

在您的項目中添加此類,並使用此方法。

希望這將幫助您獲得FireBase圖像,而無需獲取該圖像的NSURL。

暫無
暫無

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

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