2017-04-07 55 views
1

這是我的情況,(請注意我開始在iOS開發:))。在swift/ios上異步加載映像?

我有一個feed列表,其中數據是從firebase進行beign提取的,每個提要在表格視圖中獲取圖像和標題。

我正在尋找一種方式,即如果用戶回來沒有任何互聯網連接的應用程序,仍然顯示這些數據,已顯示在那之前。

目前,我用這個來顯示數據:

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的功能,這是一個好的開始?

非常感謝您的時間!

+0

你試過我的回答嗎? –

回答

1

使用核心數據存儲圖像或

如果您可能不希望存儲核心數據這些圖片,因爲如果數據集變得太大,這會影響性能。最好將圖像寫入文件。

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

希望有幫助。