ios - Swift scaleAspectFill 只有宽度

标签 ios swift

我在 Swift 中有一个 UIImageView

let background_image: UIImageView = {
    let v = UIImageView()
    v.image = #imageLiteral(resourceName: "basketball")
    v.contentMode = .scaleToFill
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

我想在这里做的是让图像填充背景而不变小或变大。 假设这张图片是背景 enter image description here

我想要做的是获取此图像的 100x100 像素 并仅显示它。就像在这个image.

最佳答案

要从中心裁剪图像,您必须将内容模式设置为居中,并允许图像剪辑绑定(bind)以防止 ImageView 大小

imageView.contentMode = .center /* take center part of image*/
imageView. clipsToBounds = true /* prevent image view size*/

关于ios - Swift scaleAspectFill 只有宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368905/

相关文章:

ios - 获取集合中的所有文档 - Firebase Firestore iOS

ios - 通过 iOS swift 照片框架保存时如何命名照片

swift - Property Observer swift 了解属性(property)的属性(property)

android - 无需基站即可在 GSM 设备之间传输数据

objective-c - 如何确定用户在 objective c 中完成了 tableview 的垂直滚动

ios - 比较sqlite表中的整数?

swift - 在 Swift 中创建 IBOutlet 结果为 'class is not constructible with ()'

ios - 解析来自 URL 的 JSON 响应给出 <null>

ios - 将 CNContact 转换为 NSData,反之亦然

swift - 将自己指定为委托(delegate)实现会导致内存泄漏吗?