简体   繁体   English

用于Tableview图像的Firebase存储URL

[英]Firebase storage url for tableview image

I am migrating across my backend from Parse :'( to Firebase and am having some issues understand Firebase's implementation around assets and referencing them. 我将后端从Parse:'()迁移到Firebase,并遇到一些问题,这些问题使他们了解Firebase围绕资产的实现并引用它们。

Essentially I need to have a JSON structure detailing some articles, each of these articles will have an image. 本质上,我需要一个详细介绍某些文章的JSON结构,这些文章中的每一个都会有一个图像。 Currently the JSON is manually added in order for the app to reference the latest articles. 当前,JSON是手动添加的,以便应用程序引用最新文章。 The images for this are manually uploaded to the storage aspect of Firebase and the file name of this is added to the JSON of the articles. 用于此目的的图像将手动上传到Firebase的存储方面,并将其文件名添加到文章的JSON。

My app calls the json in order to populate a table view with a label for the heading of the article and an image in the same cell. 我的应用程序调用json,以便在表格视图中填充文章标题标签和同一单元格中的图片。

The problem I am having is retrieving the image URL for the storage assets in order to load the image into the tableview - 我遇到的问题是要检索存储资产的图像URL,以便将图像加载到tableview中-

First I call the articles data from the database 首先,我从数据库中调用商品数据

  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);

    }];

From the return snapshot is converted into a model object - where the article heading is set to the model object as well as the image URL generated 从返回的快照将转换为模型对象-将文章标题设置为模型对象以及生成的图像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;

        }
    }];
    }

The problem I have with this method (apart from it feeling like a lot of code for something which feels like it should be a standard property) is that the Article title is available immediately, but the 'downloadURL' block takes longer, so I either need observe when this finishes and not reload my table until each article has finished obtaining its Image URL 这种方法的问题(除了感觉很多代码,它似乎应该是一个标准属性)是文章标题立即可用,但是“ downloadURL”块需要更长的时间,所以我要么需要观察何时完成,并且在每篇文章完成获取其图像URL之前都不要重新加载我的表

SO the question: Is there an alternative non blocking way to obtain the image URL Or is the URL reference I see in the storage portal for the URL reliable enough to use in my JSON instead of a reference to the image name (so store the URL in my DB rather than the image name) 因此,问题是:是否存在一种替代的非阻塞方式来获取图像URL,或者我在存储门户中看到的URL引用是否足够可靠,可以在JSON中使用,而不是对图像名称的引用(因此请存储URL在我的数据库而不是图像名称中)

Also for reference I am using SDWebimage for lazyloading images, but the URL is currently not present the first time the tablereloads due to the above mentioned delays in retrieving the URL 还供参考,我使用SDWebimage进行延迟加载图像,但是由于上述检索URL的延迟,当前第一次在表重新加载时URL不存在

Thanks in advance! 提前致谢!

You can convert your three lines of code to single line by replacing these, 您可以通过替换三行代码将它们转换为单行代码,

// 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"]];

to

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

Base on your requirement there is one extended class for SDWebImage which just take reference of FIRStorageReference and download your image. 根据您的要求,有一个SDWebImage扩展类,它仅引用FIRStorageReference并下载您的映像。

See below code for same: 参见以下代码:

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);
                                                 }
                                             }];

In above code you can find sd_setImageWithStorageReference method with storageRef to easily access FireBase Image . 在上面的代码中,您可以找到带有storageRef sd_setImageWithStorageReference方法,以轻松访问FireBase Image

You can find this Extended Class file from Here : https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI 您可以从此处找到此扩展类文件: https : //github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI

Add This classes in your project and use this method. 在您的项目中添加此类,并使用此方法。

Hope this will helps you to get FireBase Image without getting NSURL of that image. 希望这将帮助您获得FireBase图像,而无需获取该图像的NSURL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM