繁体   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