- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将背景图像设置为图案图像时,图像会被拉伸(stretch),所以我想将此图案图像设置为纵横比适合,如何才能做到这一点?
let Image1 = UIImage(named: KeyboardBG_image)
UIGraphicsBeginImageContext(keyboardview.frame.size);
Image1?.draw(in: keyboardview.bounds);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = UIColor(patternImage: image!)
谢谢..
最佳答案
AVFoundation
中有一种方法可以帮助您创建特定大小的矩形图像,同时保持纵横比。这将根据您提供的尺寸计算矩形尺寸,我使用了最大高度,因为原始图像是横向图像。
/* create the image at original size */
let image1 = UIImage(named: "Landscape-1276x800.jpg")!
/*
* create bounding rect with largest possible height based on width
* this will get you the aspect height for the specified width
*/
let boundingRect = CGRect(x: 0, y: 0, width: 200, height:CGFloat.greatestFiniteMagnitude)
/*
* Returns a scaled CGRect that maintains the aspect ratio specified
* by a CGSize within a bounding CGRect
*/
let aspectRect = AVMakeRect(aspectRatio: image1.size, insideRect: boundingRect)
/*
* create a UIGraphicsImageRenderer to draw the image, this replaces all
* the UIGraphicsContext stuff that was used to draw images
*/
let renderer = UIGraphicsImageRenderer(size: aspectRect.size)
/*
* WARNING be careful about the origin points, if the result of the calculation
* is an exponential value like 8.988465674311579e+307, then the image won't draw.
* To be safe I set the origin to .zero
*/
let img = renderer.image { (context) in
image1.draw(in: CGRect(origin: .zero, size: aspectRect.size))
}
关于ios - 如何在 swift 中对 UIView 的 AspectFit 背景图像进行 AspectFit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54490301/
当我将背景图像设置为图案图像时,图像会被拉伸(stretch),所以我想将此图案图像设置为纵横比适合,如何才能做到这一点? let Image1 = UIImage(named: KeyboardB
我的场景设置为.AspectFit。我在场景中心有摄像头节点并且已连接。一切都适用于 iPad 横向,但是当我旋转模拟器或在不同的设备上运行时,横向相机中的 iPad 不居中。为什么? 这是相机居中的
我有一张看起来像这样的图片 但它应该是这样的: 我添加了这一行:cameraUIButton.imageView?.contentMode = .scaleAspectFit 它不起作用但是如果我点击
我想将图像添加到 UIButton,并且还想缩放我的图像以适应 UIButton(使图像更小)。请教我怎么做。 这是我尝试过的,但它不起作用: 将图像添加到按钮并使用 setContentMode:
我有一个 TokBox iOS 应用程序,它为用户呈现屏幕共享流。如果视频流大于 View ,则会裁剪视频流。它默认设置为方面 FILL。 我找不到将其更改为方面 FIT 的方法。有谁知道这在 Tok
我正在制作一个音乐播放器应用程序,我想要在专辑封面图像上进行高斯模糊,因此我实现了下面的代码,但图像没有填充UIImageView: let bgView = UIImageView(frame: C
我有一个应用程序,用户可以将形状放置到图像上。该图像位于设置为 AspectFit 的 UIImageView 中。 UIImageView 位于 UIScrollView 中,允许缩放和平移。这些形
当我对与我的场景纵横比不匹配的设备使用 .AspectFit 缩放模式时,我试图显示场景边界之外的内容。我的场景是为 iPhone 5 和 6 宽高比 (1080 x 1920) 设置的,并且在黄色边
我的布局如下。占 View Controller 一半(白色)的 View 内的 ImageView 。考虑到顶部和底部的一些边距, ImageView 会填充容器。我将 ImageView 的缩放模
如何在将其内容模式设置为 UIViewContentModeScaleAspectFit 后调整 UIImageView 的大小,以便我可以删除顶部和下方的空白区域。请看附图 提前致谢:) 最佳答案
如何获取模式为 AspectFit 的 UIImageView 的比例因子? 也就是说,我有一个模式为 AspectFit 的 UIImageView。图像(正方形)缩放到我的 UIImageView
我想要一个向用户显示图像的 View 。通过拖动角点,我希望他能够选择裁剪矩形。 因为输入图像的尺寸可能比屏幕大,所以我想使用纵横比适合作为图像的内容模式。 我遇到的问题是,在确定裁剪矩形相对于图像原
我一直在尝试在 UIWebView 中显示一些来自网络的图像。我一直在做的是直接在 URL 上使用 loadRequest: 方法,使用 ScalesToFit = YES 和 AspectFit 模
我正在尝试获取 UIImage 的 image.size.h 和 image.size.w >UIImageView 开启了 UIViewContentModeScaleAspectFit。 我想要r
我正在使用来自样板代码(新游戏 iOS 项目)的标准全屏 SpriteKit View ,以及我何时使用 scene.scaleMode = .AspectFit 为确保场景适合,剩余(信箱)区域用黑
我正在使用 UICollectionView 放置图像。 我想在以下条件下排列所有图像。 (就像ios文件管理器) 1.AspectFit 2. 底部对齐 3.带圆角半径 这是引用图片。 使用 ima
我有一个表格 View ,其中包含可自行调整大小的单元格,我在其中显示图像。 ImageView 的内容模式是 AspectFit,这样我就可以以正确的宽高比看到整个图像。 ImageView 受限于
[UIView animateWithDuration:30.0f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:
我是一名优秀的程序员,十分优秀!