dev xoss dev xoss - 2 months ago 11
iOS Question

Swift asynch load image from directory

I have a situation where I am filling out a form and set image from library then storing in a directory.
I am able to get image from the directory but

list flickering in the middle of scroll, so I am working with method of
but not able to do.

If anybody has a solution, please let me know.

Here is my code.

import UIKit

func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL

func fileInDocumentsDirectory(filename: String) -> String {

let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

var marrEmpData : NSMutableArray!
@IBOutlet var MyTable: UITableView!
@IBAction func addEmpInfo(){


override func viewWillAppear(animated: Bool) {

func getStudentData()
marrEmpData = NSMutableArray()
marrEmpData = ModelManager.getInstance().getAllStudentData()

override func viewDidLoad() {
MyTable.delegate = self
MyTable.dataSource = self
// Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//return names.count;
return marrEmpData.count;

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:CustomCell = self.MyTable.dequeueReusableCellWithIdentifier("cells") as! CustomCell

let emp:EmpInfo = marrEmpData.objectAtIndex(indexPath.row) as! EmpInfo

let randomNum = "Image-\(indexPath.row+1).png"

let imagePathOne = fileInDocumentsDirectory(randomNum)

dispatch_async(dispatch_get_main_queue()) {
if let loadedImage = self.loadImageFromPath(imagePathOne) {
print("view Loaded Image: \(loadedImage)") = loadedImage
else { = UIImage(named: "default_user")

// user profile = emp.full_name
cell.age.text = emp.age =

return cell

// which one is tapped
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You tapped cell number \(indexPath.row).")

// load image of user
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)

if image == nil {

print("missing image at: \(path)")


print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image



You are using dispatch_async but you are dispatching to the main queue which is the thread you are already on. You should dispatch to a background thread, load the image, then dispatch to the main thread and update the UI.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))

    let image = self.loadImageFromPath(imagePathOne)

        if let loadedImage = image
            print("view Loaded Image: \(loadedImage)")
   = loadedImage
   = UIImage(named: "default_user")