ios – 为什么缩放比UIImageVIew大小更大的图像? (使用swift)

前端之家收集整理的这篇文章主要介绍了ios – 为什么缩放比UIImageVIew大小更大的图像? (使用swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图显示一个地名列表,其中包括使用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中,您将第二个单元格被第一个单元格图像覆盖.

有人可以帮我解决这个问题吗?我也把我的整个代码放在这里:

  1. import UIKit
  2.  
  3. class TableViewController: PFQueryTableViewController,UISearchBarDelegate {
  4.  
  5. @IBOutlet var searchBar: UISearchBar!
  6.  
  7.  
  8. // Initialise the PFQueryTable tableview
  9. override init(style: UITableViewStyle,className: String!) {
  10. super.init(style: style,className: className)
  11. }
  12.  
  13. required init(coder aDecoder: NSCoder) {
  14. super.init(coder: aDecoder)
  15.  
  16. // Configure the PFQueryTableView
  17. self.pullToRefreshEnabled = true
  18. self.paginationEnabled = false
  19. }
  20.  
  21. // Define the query that will provide the data for the table view
  22. override func queryForTable() -> PFQuery {
  23.  
  24. // Start the query object
  25. var query = PFQuery(className: "Places")
  26.  
  27. // query with pointer
  28. query.includeKey("mainPhoto")
  29.  
  30. // Add a where clause if there is a search criteria
  31. if searchBar.text != "" {
  32. query.whereKey("name",containsString: searchBar.text)
  33. }
  34.  
  35. // Order the results
  36. query.orderByAscending("name")
  37.  
  38. // Return the qwuery object
  39. return query
  40. }
  41.  
  42.  
  43. //override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  44. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath,object: PFObject) -> PFTableViewCell? {
  45. var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
  46. if cell == nil {
  47. cell = CustomTableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: "CustomCell")
  48. }
  49.  
  50. // Extract values from the PFObject to display in the table cell
  51. if let name = object["name"] as? String{
  52. cell.name.text = name
  53. }
  54.  
  55. // display initial image
  56. var initialThumbnail = UIImage(named: "question")
  57. cell.photo.image = initialThumbnail
  58.  
  59.  
  60. // extract image from pointer
  61. if let pointer = object["mainPhoto"] as? PFObject {
  62. cell.detail.text = pointer["photoTitle"] as? String!
  63. if let thumbnail = pointer["photo"] as? PFFile {
  64. cell.photo.file = thumbnail
  65. cell.photo.loadInBackground()
  66. }
  67. }
  68. cell.sendSubviewToBack(cell.photo)
  69.  
  70. // return the cell
  71. return cell
  72. }
  73.  
  74.  
  75.  
  76.  
  77. // In a storyboard-based application,you will often want to do a little preparation before navigation
  78. override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  79.  
  80. // Get the new view controller using [segue destinationViewController].
  81. var detailScene = segue.destinationViewController as! DetailViewController
  82.  
  83. // Pass the selected object to the destination view controller.
  84. if let indexPath = self.tableView.indexPathForSelectedRow() {
  85. let row = Int(indexPath.row)
  86. detailScene.currentObject = objects[row] as? PFObject
  87. }
  88. }
  89.  
  90. override func viewDidLoad(){
  91. super.viewDidLoad()
  92. let tapGesture = UITapGestureRecognizer(target:self,action:Selector("hideKeyboard"))
  93. tapGesture.cancelsTouchesInView = true
  94. tableView.addGestureRecognizer(tapGesture)
  95. }
  96.  
  97. func hideKeyboard(){
  98. tableView.endEditing(true)
  99. }
  100.  
  101. override func viewDidAppear(animated: Bool) {
  102.  
  103. // Refresh the table to ensure any data changes are displayed
  104. tableView.reloadData()
  105.  
  106. // Delegate the search bar to this table view class
  107. searchBar.delegate = self
  108. }
  109.  
  110. func searchBarTextDidEndEditing(searchBar: UISearchBar) {
  111.  
  112. // Dismiss the keyboard
  113. searchBar.resignFirstResponder()
  114.  
  115. // Force reload of table data
  116. self.loadObjects()
  117. }
  118.  
  119. func searchBarSearchButtonClicked(searchBar: UISearchBar) {
  120.  
  121. // Dismiss the keyboard
  122. searchBar.resignFirstResponder()
  123.  
  124. // Force reload of table data
  125. self.loadObjects()
  126. }
  127.  
  128. func searchBarCancelButtonClicked(searchBar: UISearchBar) {
  129.  
  130. // Clear any search criteria
  131. searchBar.text = ""
  132.  
  133. // Dismiss the keyboard
  134. searchBar.resignFirstResponder()
  135.  
  136. // Force reload of table data
  137. self.loadObjects()
  138. }
  139.  
  140. }

解决方法

内容模式设置为Aspect Fill,尝试将剪辑设置为true,因为内容模式方面的填充会继续填充图像视图的框架,直到框架完全填满内容,同时保持纵横比不变.在填充具有图像保持宽高比的容器的过程中,垂直或水平框架被完全填充,并且填充继续到另一个(如果水平比垂直或反之亦然)部分被完全填充.因此,垂直或水平方向上的第一个填充部分将超出边界,并且内容将在图像视图的框架外部可见.要剪辑额外的内容,我们需要使用imageView的clipsToBounds属性设置为true来剪切额外的部分
  1. cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
  2. cell.photo.clipsToBounds = true

猜你在找的iOS相关文章