为什么缩放比UIImageVIew尺寸更大?(使用swift)

Gib*_*ran 23 ios parse-platform swift pfimageview

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

我设法显示图像.不幸的是,当我将UIImageView模式更改为Aspect填充时,图像变得比它应该更大.

这是图片:

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()
    }

}
Run Code Online (Sandbox Code Playgroud)

San*_*ani 85

将内容模式设置为Aspect Fill,尝试将剪辑设置为绑定为true,原因是内容模式aspect fill继续填充图像视图的框架,直到框架完全填充内容也保持aspect ratio完整.在用图像保持纵横比填充容器的过程中,垂直或水平框架被完全填充,并且填充继续直到另一个(如果水平而不是垂直或反之亦然)部分被完全填充.因此,横跨垂直或水平的第一个填充部分将超出边界,并且内容将在图像视图的框架外可见.要剪切额外的内容,我们需要使用imageView的clipsToBounds属性设置剪辑额外的部分true

cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)


Avi*_*h B 11

在此输入图像描述

参考:另一篇文章的答案,为您提供明确的见解 - /sf/answers/995442381/

  • 难道这不能只是问题下方的评论吗?您还没有真正提供答案,而只是参考。这就是人们通常做出的小有帮助的贡献的地方,这些贡献并不真正符合海报提供的技术答案。 (2认同)

Rap*_*toX 9

或者对于喜欢Interface Builder的人来说,您可以Clip Subviews在选择时检查图像视图Aspect Fill,然后它不会调整图像视图的大小,但仍保持相同的宽高比.

在此输入图像描述