ios - 在 swift/ios 上异步加载图像?

标签 ios swift firebase

这是我的情况,(请注意我开始使用 iOS 开发 :))。

我有一个 feed 列表,其中的数据是从 firebase 获取的,每个 feed 在表格 View 中获取图像和标题。

我正在寻找一种方法,如果用户在没有任何互联网连接的情况下返回到该应用程序,仍然显示那些之前显示的数据。

目前,我正在使用它来显示数据:

import UIKit
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import SwiftKeychainWrapper
import SwiftyJSON

var posts = [Post]()
var selectedIndexPath: Int = 10

class FeedVC: UIViewController, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDelegate {

@IBOutlet weak var feedTableView: UITableView!

private var readPosts: ObserveTask?

override func viewDidLoad() {
    super.viewDidLoad()

    feedTableView.dataSource = self
    feedTableView.delegate = self

    readPosts = Post.observeList(from: Post.parentReference.queryOrdered(byChild: "privacy").queryEqual(toValue: false))

    {
        observedPosts in

        posts = observedPosts.reversed()
        self.feedTableView.reloadData()
    }
}

override var prefersStatusBarHidden: Bool {
    return true
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  {
    let cell = self.feedTableView.dequeueReusableCell(withIdentifier: "MessageCell")! as UITableViewCell


 let imageView = cell.viewWithTag(1) as! UIImageView



    let titleLabel = cell.viewWithTag(2) as! UILabel
    let linkLabel = cell.viewWithTag(3) as! UILabel

    titleLabel.text = posts[indexPath.row].title
    titleLabel.numberOfLines = 0

    linkLabel.text = posts[indexPath.row].link
    linkLabel.numberOfLines = 0



    Storage.getImage(with: posts[indexPath.row].imageUrl){
        postPic in
        imageView.image = postPic
    }

    return cell

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard posts[indexPath.row].title != "COMING SOON" else {
        return
    }
           selectedIndexPath = indexPath.row
          self.performSegue(withIdentifier: "push", sender: self)
            self.feedTableView.reloadData()    }


}

这怎么可能?我一直在寻找 NSUR 功能,这是一个好的开始吗?

非常感谢您的宝贵时间!

最佳答案

使用核心数据存储图片或

如果您可能不想将这些图像存储在 Core Data 中,因为如果数据集变得太大,这会影响性能。最好将图像写入文件。

NSData *pngData = UIImagePNGRepresentation(image);

这会提取您捕获的图像的 PNG 数据。从这里,您可以将其写入文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file

以后再看也是一样的。像上面那样构建路径,然后:

NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];

您可能想要做的是创建一个为您创建路径字符串的方法,因为您不希望该代码随处可见。它可能看起来像这样:

- (NSString *)documentsPathForFileName:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];

    return [documentsPath stringByAppendingPathComponent:name]; 
}

希望对您有所帮助。

关于ios - 在 swift/ios 上异步加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43279334/

相关文章:

ios - NSMutableArray addObjects 崩溃

ios - iTunes connect 更新仅限于某些国家/地区?

ios - Swift 3 发布后,Swift 2 应用程序可以正常运行吗?

ios - Swift 无法从 NSIndexPath 对象调用部分/行

ios - UITableView 不显示单元格

ios - 使用检索到 Swift 数据类型的 JSON 数据

firebase - Flutter:我们如何在桌面应用程序中使用 Firebase 数据库

php - 使用 PHP 执行 cURL 发送推送通知

ios - 滚动后 UITableView 单元格正确

iphone - 使用自定义 segue 动画将一个 View 替换为另一个 View ?