ios 为什么缩放以填充比 UIImageVIew 尺寸更大的图像?(使用迅捷)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30073097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why scale to fill give bigger image than UIImageVIew size? (using swift)
提问by Gibran
I'm trying to showing a list of place name, including it's photo using PFQueryTableViewController
. It's included in ParseUI SDK from parse.com
我正在尝试显示一个地名列表,包括使用PFQueryTableViewController
. 它包含在ParseUI SDK从parse.com
I have managed to show the image. Unfortunately, when I change the UIImageView mode to Aspect fill, the image is become bigger than it should be.
我已经设法显示图像。不幸的是,当我将 UIImageView 模式更改为 Aspect fill 时,图像变得比应有的大。
here are the pictures:
以下是图片:
https://dl.dropboxusercontent.com/u/86529526/pic_normal.pnghttps://dl.dropboxusercontent.com/u/86529526/pic_error.png
https://dl.dropboxusercontent.com/u/86529526/pic_normal.png https://dl.dropboxusercontent.com/u/86529526/pic_error.png
in pic_normal
, you will see two cell, with two normal image.
in pic_error
, you will the second cell was being overlaid by the first cell image.
在 中pic_normal
,您将看到两个单元格,带有两个正常图像。在 中pic_error
,您将第二个单元格被第一个单元格图像覆盖。
can anyone help me to solve this problem? I also put my whole code here :
谁能帮我解决这个问题?我也把我的整个代码放在这里:
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()
}
}
回答by Sanjay Mohnani
With content mode set to Aspect Fill
, try setting clips to bounds to true as well, reason being the content mode aspect fill
keeps on filling the frame of the image view till the frame is fully filled with content also keeping the aspect ratio
intact. In the process of filling the container with image maintaining aspect ratio, either vertical or horizontal frame is fully filled, and the filling is continued till the other (if horizontal than vertical or vise versa) portion is fully filled. Thus the first filled portion either across vertical or horizontal will go out of bounds and the content will be visible outside the frame of the image view. To clip the extra content we need to clip the extra portion using imageView's clipsToBounds
property set to true
将内容模式设置为Aspect Fill
,尝试将剪辑的边界设置为 true ,原因是内容模式aspect fill
不断填充图像视图的框架,直到框架完全充满内容并保持aspect ratio
完整。在以图像保持纵横比填充容器的过程中,垂直或水平框架被完全填充,并且继续填充直到另一个(如果水平比垂直或反之亦然)部分被完全填充。因此,垂直或水平的第一个填充部分将超出边界,并且内容将在图像视图的框架外可见。要剪辑额外的内容,我们需要使用clipsToBounds
设置为imageView 的属性来剪辑额外的部分true
cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true
回答by Avinash B
Reference: an answer in another post which gives you clear insight - https://stackoverflow.com/a/14220605/3021573
参考:另一篇文章中的答案,让您有清晰的洞察力 - https://stackoverflow.com/a/14220605/3021573
回答by RaptoX
Alternatively for guys who prefer Interface Builder, you can check the Clip Subviews
at your image view when you choose Aspect Fill
, then it will not resize your Image Viewbut still keep the same aspect ratio.
或者,对于喜欢Interface Builder 的人,您可以Clip Subviews
在选择时检查图像视图Aspect Fill
,然后它不会调整图像视图的大小,但仍保持相同的纵横比。
回答by Mukesh
If you want to maintain the aspect ratio with varying width ,then use AspectFill and also use the imageview property clipstoBounds,it will not spread the imageview beyond the frame
如果你想保持不同宽度的纵横比,那么使用 AspectFill 并使用 imageview 属性 clipstoBounds,它不会将 imageview 扩展到框架之外
回答by Jakub Vano
From apple doc:
来自苹果文档:
UIViewContentModeScaleToFill
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary
如有必要,通过更改内容的纵横比来缩放内容以适应自身的大小
UIViewContentModeScaleAspectFill
UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view's bounds.
缩放内容以填充视图的大小。内容的某些部分可能会被裁剪以填充视图的边界。
So your options are to use
所以你的选择是使用
UIViewContentModeScaleToFill
, and possibly change the aspect ratio of displayed image,UIViewContentModeScaleAspectFill
, and useclipToBounds = YES
on yourPFImageView
, to clip portions of image out of bounds.
UIViewContentModeScaleToFill
,并可能改变显示图像的纵横比,UIViewContentModeScaleAspectFill
, 并clipToBounds = YES
在您的PFImageView
, 上使用以将图像的部分剪切到边界之外。