gpt4 book ai didi

php - Swift 3 - 从 mySQL 数据库中提取图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:02 25 4
gpt4 key购买 nike

我目前正在设计一个具有新闻源的应用程序。图片和文本存储在我们的数据库中,但我无法提取和显示图片。我拥有的代码应该可以工作,但我收到一个 fatal error ,提示“THREAD 1: EXC_BAD_INSTRUCTION”

let imageData = try? Data(contentsOf: url)
let image = UIImage(data: imageData!)!

并且编译器显示此消息 - “ fatal error :在展开可选值时意外发现 nil”。

我在这段代码中遇到了错误:

if!path!.isEmpty {
let url = URL(string: path!)!
let imageData = try? Data(contentsOf: url)
let image = UIImage(data: imageData!)!
self.images.append(image)
} else {
let image = UIImage()
self.images.append(image)
}

作为编程的新手,我已经坚持了很长一段时间。如果您有任何建议,请告诉我!谢谢!

此外,我要检索的照片是 .jpg,不确定这是否重要。

(如果需要,这里是其余代码)

import UIKit

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var activities = [AnyObject]()
var images = [UIImage]()

@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

//tableView.contentInset = UIEdgeInsetsMake(2, 0, 0, 0)


}


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

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

let post = activities[indexPath.row]
let image = images[indexPath.row]
let username = post["username"] as? String
let text = post["text"] as? String

cell.titleLbl.text = text
cell.userLbl.text = username
cell.activityImage.image! = image
//cell.dateLbl.text = activities[indexPath.row]
//cell.activityImage.image = images[indexPath.row]


return cell
}

override func viewWillAppear(_ animated: Bool) {
loadActivities()
}


func loadActivities() {
let id = user!["id"] as! String
let url = URL(string: "https://cgi.soic.indiana.edu/~team7/posts.php")!

var request = URLRequest(url: url)

request.httpMethod = "POST"

let body = "id=\(id)&text=&uuid="
request.httpBody = body.data(using: String.Encoding.utf8)

URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {

if error == nil {

do {

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

// clean up
self.activities.removeAll(keepingCapacity: false)
self.images.removeAll(keepingCapacity: false)
self.tableView.reloadData()

guard let parseJSON = json else {
print("Error while parsing")
return
}

guard let posts = parseJSON["posts"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}


self.activities = posts

for i in 0 ..< self.activities.count {

let path = self.activities[i]["path"] as? String

if !path!.isEmpty {
let url = URL(string: path!)!
let imageData = try? Data(contentsOf: url)
let image = UIImage(data: imageData!)!
self.images.append(image)
} else {
let image = UIImage()
self.images.append(image)
}

}



self.tableView.reloadData()


} catch {
}

} else {
}

})

}.resume()

}

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


}

extension NSMutableData {

func appendString(_ string : String) {

let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)

}

}

最佳答案

你为什么要做这一切?从服务器获取图像的 URL,然后使用 SDWebImage 在 cellForRowAtIndexPath 中异步加载它们。这是一个图书馆,你可以在这里找到它。 https://github.com/rs/SDWebImage .这样您就不需要将图像存储在数组中,而且加载速度会更快。

或者您也可以实现自己的图像异步加载。

关于php - Swift 3 - 从 mySQL 数据库中提取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43173762/

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