用于tableview图像的Firebase存储网址

时间:2016-07-27 11:40:09

标签: ios objective-c image firebase firebase-storage

我正在从Parse迁移到后端:'(到Firebase,我遇到一些问题,了解Firebase围绕资产的实施情况并引用它们。

基本上我需要有一个JSON结构,详细说明一些文章,这些文章中的每一篇都会有一个图像。目前,手动添加JSON以便应用程序引用最新文章。将此图像手动上传到Firebase的存储方面,并将其文件名添加到文章的JSON中。

我的应用程序调用json以填充表格视图,其中包含文章标题和同一单元格中的图像的标签。

我遇到的问题是检索存储资产的图片网址,以便将图片加载到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&#39 ;块需要更长的时间,所以我需要观察完成后不要重新加载我的表,直到每篇文章完成获取其图像URL

所以问题是: 是否有另一种非阻塞方式来获取图像URL 或者是我在存储门户中看到的URL引用,该URL足够可靠,可以在我的JSON中使用而不是对图像名称的引用(因此将URL存储在我的数据库而不是图像名称中)

另外作为参考,我使用SDWebimage来延迟加载图片,但由于上述检索URL的延迟,第一次表格加载时URL目前不存在

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以将三行代码转换为单行代码,

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

答案 1 :(得分:0)

根据您的要求,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);
                                                 }
                                             }];

在上面的代码中,您可以使用sd_setImageWithStorageReference找到storageRef方法,以便轻松访问FireBase Image

您可以在此处找到此扩展类文件:https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI

在项目中添加此类并使用此方法。

希望这可以帮助您获取FireBase图像,而无需获取该图像的NSURL。

相关问题