UIImageView 不显示图像
UIImageView Not Displaying Image
全部,我是 swift 的新手,运行 遇到了从 json 负载加载图像并在 [=25= 中显示结果的问题] 查看单元格。
这是我收到的错误。我的标签加载正常,甚至根据情节提要中的约束设置为图像容器分配了 space。
错误提示我配置不正确。我正在寻找一些帮助来解释错误以及我可能会尝试做些什么来隔离问题并显示图像。
控制台输出:
2020-07-11 16:23:01.467179-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>",
"<NSLayoutConstraint:0x280388eb0 UIImageView:0x14310f910.leading == UITableViewCellContentView:0x14310ecc0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x280388dc0 UIImageView:0x14310f910.top == UITableViewCellContentView:0x14310ecc0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388d70 UITableViewCellContentView:0x14310ecc0.bottomMargin == UIImageView:0x14310f910.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388d20 UIImageView:0x14310f910.centerX == UITableViewCellContentView:0x14310ecc0.centerX (active)>",
"<NSLayoutConstraint:0x280388a50 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388820 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x14310ecc0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803888c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x14310ecc0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388aa0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388af0 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-07-11 16:23:01.475387-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>",
"<NSLayoutConstraint:0x280388370 UIImageView:0x143111b60.leading == UITableViewCellContentView:0x1431119c0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x2803882d0 UIImageView:0x143111b60.top == UITableViewCellContentView:0x1431119c0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388280 UITableViewCellContentView:0x1431119c0.bottomMargin == UIImageView:0x143111b60.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388230 UIImageView:0x143111b60.centerX == UITableViewCellContentView:0x1431119c0.centerX (active)>",
"<NSLayoutConstraint:0x28038bcf0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x2803a7840 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x1431119c0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803a75c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x1431119c0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388000 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x280388050 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x1431119c0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
这是我为处理 Table 视图单元格而创建的代码:
//
// ModelTableView.swift
import Foundation
import UIKit
import Alamofire
import SwiftKeychainWrapper
import SwiftyJSON
class ModelTableView: UITableViewController{
let defaults = UserDefaults.standard
lazy var url = URL(string:"http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")! )
var models : JSON = [:]
override func viewDidLoad() {
super.viewDidLoad()
guard let username = defaults.string(forKey: "username") else { return }
let authToken: String? = KeychainWrapper.standard.string(forKey: "authToken")
print("table view controller activated")
print(username)
print(authToken)
print("BEFORE DOWNLOAD")
print(models)
newDownloadJSON()
print("AFTER DOWNLOAD")
print(models)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
func newDownloadJSON(){
let url: String = "http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")!
var request = URLRequest(url: NSURL(string: url)! as URL)
let token: String? = KeychainWrapper.standard.string(forKey: "authToken")
// Your request method type Get or post etc according to your requirement
request.httpMethod = "GET"
request.setValue("Bearer "+token!, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON { response in
print("Downloaded Data")
// Your required functionality here
let modelJSON = JSON(response.result.value)
print("MODELSE AVAILABLE")
print(modelJSON.count)
print("BEFORE SETTING VALUE FUNCTION")
print(self.models)
self.models = modelJSON;
print(modelJSON[0]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"])
print(modelJSON[0]["title"]["rendered"]);
print("AFTER DOWNLOAD INSIDE ALAMO FUNCTION")
print(self.models)
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("PRINTING NUMBER OF VALUES IN TABLE")
print(models.count)
return models.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:"ModelCell") as? ModelCell else { return UITableViewCell() }
cell.modName.text = self.models[indexPath.row]["title"]["rendered"].stringValue
if let imageURL = URL(string: "http://serverIP/wp-content/uploads" + self.models[indexPath.row]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"].stringValue){
DispatchQueue.global().async{
let data = try? Data(contentsOf: imageURL)
if let data = data{
let image = UIImage(data: data)
DispatchQueue.main.async{
cell.modImg.image = image
}
}
}
}
return cell
}
}
上面的代码很好,路径引用不正确的菜鸟。
全部,我是 swift 的新手,运行 遇到了从 json 负载加载图像并在 [=25= 中显示结果的问题] 查看单元格。
这是我收到的错误。我的标签加载正常,甚至根据情节提要中的约束设置为图像容器分配了 space。
错误提示我配置不正确。我正在寻找一些帮助来解释错误以及我可能会尝试做些什么来隔离问题并显示图像。
控制台输出:
2020-07-11 16:23:01.467179-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>",
"<NSLayoutConstraint:0x280388eb0 UIImageView:0x14310f910.leading == UITableViewCellContentView:0x14310ecc0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x280388dc0 UIImageView:0x14310f910.top == UITableViewCellContentView:0x14310ecc0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388d70 UITableViewCellContentView:0x14310ecc0.bottomMargin == UIImageView:0x14310f910.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388d20 UIImageView:0x14310f910.centerX == UITableViewCellContentView:0x14310ecc0.centerX (active)>",
"<NSLayoutConstraint:0x280388a50 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388820 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x14310ecc0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803888c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x14310ecc0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388aa0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388af0 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-07-11 16:23:01.475387-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>",
"<NSLayoutConstraint:0x280388370 UIImageView:0x143111b60.leading == UITableViewCellContentView:0x1431119c0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x2803882d0 UIImageView:0x143111b60.top == UITableViewCellContentView:0x1431119c0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388280 UITableViewCellContentView:0x1431119c0.bottomMargin == UIImageView:0x143111b60.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388230 UIImageView:0x143111b60.centerX == UITableViewCellContentView:0x1431119c0.centerX (active)>",
"<NSLayoutConstraint:0x28038bcf0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x2803a7840 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x1431119c0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803a75c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x1431119c0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388000 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x280388050 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x1431119c0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
这是我为处理 Table 视图单元格而创建的代码:
//
// ModelTableView.swift
import Foundation
import UIKit
import Alamofire
import SwiftKeychainWrapper
import SwiftyJSON
class ModelTableView: UITableViewController{
let defaults = UserDefaults.standard
lazy var url = URL(string:"http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")! )
var models : JSON = [:]
override func viewDidLoad() {
super.viewDidLoad()
guard let username = defaults.string(forKey: "username") else { return }
let authToken: String? = KeychainWrapper.standard.string(forKey: "authToken")
print("table view controller activated")
print(username)
print(authToken)
print("BEFORE DOWNLOAD")
print(models)
newDownloadJSON()
print("AFTER DOWNLOAD")
print(models)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
func newDownloadJSON(){
let url: String = "http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")!
var request = URLRequest(url: NSURL(string: url)! as URL)
let token: String? = KeychainWrapper.standard.string(forKey: "authToken")
// Your request method type Get or post etc according to your requirement
request.httpMethod = "GET"
request.setValue("Bearer "+token!, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON { response in
print("Downloaded Data")
// Your required functionality here
let modelJSON = JSON(response.result.value)
print("MODELSE AVAILABLE")
print(modelJSON.count)
print("BEFORE SETTING VALUE FUNCTION")
print(self.models)
self.models = modelJSON;
print(modelJSON[0]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"])
print(modelJSON[0]["title"]["rendered"]);
print("AFTER DOWNLOAD INSIDE ALAMO FUNCTION")
print(self.models)
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("PRINTING NUMBER OF VALUES IN TABLE")
print(models.count)
return models.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:"ModelCell") as? ModelCell else { return UITableViewCell() }
cell.modName.text = self.models[indexPath.row]["title"]["rendered"].stringValue
if let imageURL = URL(string: "http://serverIP/wp-content/uploads" + self.models[indexPath.row]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"].stringValue){
DispatchQueue.global().async{
let data = try? Data(contentsOf: imageURL)
if let data = data{
let image = UIImage(data: data)
DispatchQueue.main.async{
cell.modImg.image = image
}
}
}
}
return cell
}
}
上面的代码很好,路径引用不正确的菜鸟。