首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIImageView不显示图像

UIImageView不显示图像
EN

Stack Overflow用户
提问于 2020-07-12 06:06:43
回答 1查看 26关注 0票数 0

总之,我是swift的新手,在从json有效负载加载图像并在表视图单元中显示结果时遇到了问题。

这是我收到的错误。我的标签加载得很好,甚至还有基于故事板中设置的约束为图像容器分配的空间。

这个错误告诉我有些东西没有正确配置。我正在寻找一些相关的错误和我可能会尝试做的事情,以便隔离问题并显示图像。

控制台输出:

代码语言:javascript
运行
复制
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.

这是我为处理表格视图单元格而创建的代码:

代码语言:javascript
运行
复制
//
//  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
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-12 06:19:19

上面的代码是好的,noob有一个不正确的路径引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62855103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档