我正在尝试显示地名列表,包括使用PFQueryTableViewController的照片。它包含在parse.com的ParseUI SDK中

我设法显示了图像。不幸的是,当我将UIImageView模式更改为Aspect Fill时,图像变得比原来应该的大。

这是图片:

https://dl.dropboxusercontent.com/u/86529526/pic_normal.png
https://dl.dropboxusercontent.com/u/86529526/pic_error.png

pic_normal中,您将看到两个单元格和两个正常图像。
pic_error中,您将第二个单元格覆盖在第一个单元格图像中。

谁能帮我解决这个问题?我也将整个代码放在这里:

import UIKit

class TableViewController: PFQueryTableViewController, UISearchBarDelegate {

    @IBOutlet var searchBar: UISearchBar!


    // Initialise the PFQueryTable tableview
    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        // Configure the PFQueryTableView
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
    }

    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery {

        // Start the query object
        var query = PFQuery(className: "Places")

        // query with pointer
        query.includeKey("mainPhoto")

        // Add a where clause if there is a search criteria
        if searchBar.text != "" {
            query.whereKey("name", containsString: searchBar.text)
        }

        // Order the results
        query.orderByAscending("name")

        // Return the qwuery object
        return query
    }


    //override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell? {
        var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
        if cell == nil {
            cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
        }

        // Extract values from the PFObject to display in the table cell
        if let name = object["name"] as? String{
            cell.name.text = name
        }

        // display initial image
        var initialThumbnail = UIImage(named: "question")
        cell.photo.image = initialThumbnail


        // extract image from pointer
        if let pointer = object["mainPhoto"] as? PFObject {
            cell.detail.text = pointer["photoTitle"] as? String!
            if let thumbnail = pointer["photo"] as? PFFile {
                cell.photo.file = thumbnail
                cell.photo.loadInBackground()
            }
        }
        cell.sendSubviewToBack(cell.photo)

        // return the cell
        return cell
    }




    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].
        var detailScene = segue.destinationViewController as! DetailViewController

        // Pass the selected object to the destination view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let row = Int(indexPath.row)
            detailScene.currentObject = objects[row] as? PFObject
        }
    }

    override func viewDidLoad(){
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target:self, action:Selector("hideKeyboard"))
        tapGesture.cancelsTouchesInView = true
        tableView.addGestureRecognizer(tapGesture)
    }

    func hideKeyboard(){
        tableView.endEditing(true)
    }

    override func viewDidAppear(animated: Bool) {

        // Refresh the table to ensure any data changes are displayed
        tableView.reloadData()

        // Delegate the search bar to this table view class
        searchBar.delegate = self
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        // Clear any search criteria
        searchBar.text = ""

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }

}

最佳答案

在将内容模式设置为Aspect Fill的情况下,也尝试将剪辑设置为true,因为内容模式aspect fill会继续填充 ImageView 的框架,直到该框架完全填充了内容,也使aspect ratio保持完整。在以图像保持高宽比填充容器的过程中,垂直或水平框都被完全填充,并且填充持续到另一部分(如果水平而不是垂直,反之亦然)则完全填充。因此,垂直或水平方向上的第一个填充部分将超出范围,并且内容将在 ImageView 框架的外部可见。要裁剪多余的内容,我们需要使用imageView的clipsToBounds属性设置为true裁剪多余的部分

cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true

关于ios - 为什么缩放以提供大于UIImageVIew大小的图像? (使用 swift ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30073097/

10-12 00:19
查看更多