Ios 为什么缩放以填充比UIImageVIew尺寸更大的图像?(使用swift)
我正在尝试使用Ios 为什么缩放以填充比UIImageVIew尺寸更大的图像?(使用swift),ios,swift,parse-platform,pfimageview,Ios,Swift,Parse Platform,Pfimageview,我正在尝试使用PFQueryTableViewController显示地名列表,包括它的照片。它包含在来自的ParseUI SDK中 我成功地展示了这幅图像。不幸的是,当我将UIImageView模式更改为Aspect fill时,图像变得比应该的大 以下是照片: 在pic_normal中,您将看到两个单元格,两个普通图像。 在picu error中,您将看到第二个单元格被第一个单元格图像覆盖 有人能帮我解决这个问题吗?我还将我的全部代码放在这里: import UIKit class T
PFQueryTableViewController
显示地名列表,包括它的照片。它包含在来自的ParseUI SDK中
我成功地展示了这幅图像。不幸的是,当我将UIImageView模式更改为Aspect fill时,图像变得比应该的大
以下是照片:
在pic_normal
中,您将看到两个单元格,两个普通图像。
在picu 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()
}
}
如果要保持不同宽度的纵横比,请使用AspectFill并同时使用imageview属性clipstoBounds,它将不会将imageview扩展到帧之外: UIViewContentModeScaleToFill 通过更改外观来缩放内容以适应其自身的大小 如有必要,内容物的比例 UIViewContentModeScaleAspectFill 缩放内容以填充视图的大小。部分 可以剪裁内容以填充视图的边界 所以你的选择是使用
,并可能更改显示图像的纵横比UIViewContentModeScaleToFill
,并在您的UIViewContentModeScaleAspectFill
上使用PFImageView
,将图像的部分剪裁到边界之外cliptobunds=YES
- 将内容模式设置为
纵横比填充
,同时尝试将剪辑边界设置为true,原因是内容模式纵横比填充
会继续填充图像视图的帧,直到帧完全填充内容,同时保持纵横比
完好无损。在用图像保持纵横比填充容器的过程中,垂直或水平框架被完全填充,并且填充继续,直到另一部分(如果水平而不是垂直或反之亦然)被完全填充。因此,垂直或水平方向上的第一个填充部分将超出边界,并且内容将在图像视图的框架外可见。要剪辑额外内容,我们需要使用imageView的clipsToBounds
属性设置为true
cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true
或者,对于喜欢界面生成器的人来说,当你选择“纵横比填充”时,你可以在你的图像视图中检查
剪辑子视图
,这样它就不会调整你的图像视图的大小,但仍然保持相同的纵横比
参考:另一篇文章中的答案让你有了清晰的见解-使UIImageView模式适合方面我想填充UIImageView,但同时保持纵横比。参考此答案你知道方面填充和方面适合之间的区别。当所有UIViewContentModes都无法满足时,此答案帮助了我。-这难道不是问题下面的一个评论吗?你没有真正提供答案,只是提供了一个参考。人们通常会在这里做出一些小的有用的贡献,但这些贡献实际上并不符合海报提供的技术答案。对此表示抱歉,但答案确实很长。此外,我认为最好还是把信用留给写这封信的人。我不熟悉堆栈溢出。请建议我现在能做什么。@AvinashB你可以直接引用这篇文章而不是答案。这样的话,你仍然可以为作者保留学分。更好的做法是:从文章中提取重要部分,然后在这里给出答案,并提及信息来源。