ios - 为什么缩放以填充给出比 UIImageVIew 尺寸更大的图像? (使用 swift )

标签 ios swift parse-platform pfimageview

我正在尝试显示地名列表,包括使用 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,尝试将 clips to bounds 也设置为 true,原因是内容模式 aspect fill 不断填充图像的框架查看直到框架完全充满内容,同时保持宽高比不变。在以图像保持宽高比填充容器的过程中,垂直或水平框架被完全填充,并且继续填充直到另一个(如果水平而不是垂直或反之亦然)部分被完全填充。因此,垂直或水平方向的第一个填充部分将超出边界,并且内容将在 ImageView 的框架之外可见。要剪切额外的内容,我们需要使用 imageView 的 clipsToBounds 属性设置为 true

来剪切额外的部分
cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true

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

相关文章:

ios - 在 iOS Swift 中设置按钮事件颜色的简单方法

ios - URL初始化(字符串:relativeTo:) works wrong

ios - ParseTwitterUtils 内部服务器错误

ios - UIActivityViewController 上缺少关闭按钮

iOS- UIWebView 和下载的代码

ios - Admob 导致 Cocos2d 触摸延迟

ios - 我可以在不将联系人保存到 contactStore 的情况下使用 View CNContactViewController 吗?

ios - 使用指针查询多个 (n=7) 类

ios - 从 parse.com 检索 IOS 应用程序的 PFuser 时遇到问题

iphone - Viddy 如何实现可下载过滤器?