gpt4 book ai didi

ios - Swift 异步从目录加载图像

转载 作者:行者123 更新时间:2023-11-30 13:05:30 25 4
gpt4 key购买 nike

我遇到一种情况,我正在填写表格并从库中设置图像,然后将其存储在目录中。我能够从目录中获取图像,但 tableview 列表在滚动中间闪烁,因此我正在使用 dispatch_async 方法,但无法执行此操作。

如果有人有解决方案,请告诉我。

这是我的代码。

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) {
self.getStudentData()
}

func getStudentData()
{
marrEmpData = NSMutableArray()
marrEmpData = ModelManager.getInstance().getAllStudentData()
MyTable.reloadData()
}



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

override func didReceiveMemoryWarning() {
super.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)")
cell.photo.image = loadedImage
}
else {
cell.photo.image = UIImage(named: "default_user")
}
}

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


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? {
print("image-----\(path)")
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
}

}

最佳答案

您正在使用dispatch_async,但您正在分派(dispatch)到主队列,这是您已经所在的线程。您应该分派(dispatch)到后台线程,加载图像,然后分派(dispatch)到主线程并更新 UI。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))
{

let image = self.loadImageFromPath(imagePathOne)

dispatch_async(dispatch_get_main_queue())
{
if let loadedImage = image
{
print("view Loaded Image: \(loadedImage)")
cell.photo.image = loadedImage
}
else
{
cell.photo.image = UIImage(named: "default_user")
}
}

}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com