我试图改变图像比例在一个UIImageView,我希望它是小,并保持其比例。我试过:
cameraPhoto.image?.scale = CGFloat(0.5)
字符串但不管用我得到:无法分配给属性:“scale”是一个只获取属性我想要这个
的数据而不是这样:
的如何缩小相机图片大小(如上面的示例)?
z4iuyo4d1#
您可以使用此扩展:
extension UIImage { func imageWithSize(size: CGSize) -> UIImage? { UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale); let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height); draw(in: rect) let resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage } func imageWithScale(scale: CGFloat) -> UIImage? { let width = self.size.width * scale let height = self.size.height * scale return self.imageWithSize(size: CGSize(width: width, height: height)) } }
字符串并在您的图像中使用此函数,例如:
let img = yourImage?.imageWithScale(scale: 0.5)
型您还应该使用UIImageView属性:.contentMode,例如:
imageView?.contentMode = .scaleAspectFill //or what ever you want(need)
型如果你想在你图像中增加额外的边距,你可以在你的UIImage扩展中添加这个函数:
func imageWithSize(size: CGSize, extraMargin: CGFloat) -> UIImage? { let imageSize = CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2) UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale); let drawingRect = CGRect(x: extraMargin, y: extraMargin, width: size.width, height: size.height) draw(in: drawingRect) let resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage }
型
4uqofj5v2#
在UIView中嵌入图像视图的快速示例:
UIView
class CameraView: UIView { override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } private func commonInit() { backgroundColor = .white // if we want thin, bold, regular, etc let imgWeight = UIImage.SymbolConfiguration(weight: .bold) // safely unwrap optional system image named "camera" if let img = UIImage(systemName: "camera", withConfiguration: imgWeight) { // create image view let imgView = UIImageView(image: img) // if we want a specific color imgView.tintColor = .black // we want aspect fit imgView.contentMode = .scaleAspectFit imgView.translatesAutoresizingMaskIntoConstraints = false addSubview(imgView) NSLayoutConstraint.activate([ // center the image view imgView.centerXAnchor.constraint(equalTo: centerXAnchor), imgView.centerYAnchor.constraint(equalTo: centerYAnchor), // let's make it 60% of the size of this view imgView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.6), imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor), ]) } // add shadow to self layer.shadowOffset = .zero layer.shadowColor = UIColor.black.cgColor layer.shadowRadius = 3 layer.shadowOpacity = 0.4 } override func layoutSubviews() { super.layoutSubviews() // make self a circle // assumes this view has a 1:1 size ratio layer.cornerRadius = bounds.width * 0.5 } }
字符串看起来像这样(尺寸60 x 60):
的数据和一个示例视图控制器,将其覆盖在标签上:
class CameraViewTestVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground let label = UILabel() label.backgroundColor = .cyan label.numberOfLines = 0 label.textAlignment = .center label.text = "Sample\nLabel" label.font = .systemFont(ofSize: 40.0) let camView = CameraView() label.translatesAutoresizingMaskIntoConstraints = false camView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(label) view.addSubview(camView) let g = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ label.centerXAnchor.constraint(equalTo: g.centerXAnchor), label.centerYAnchor.constraint(equalTo: g.centerYAnchor), camView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: -30.0), camView.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: -30.0), camView.widthAnchor.constraint(equalToConstant: 60.0), camView.heightAnchor.constraint(equalTo: camView.widthAnchor, multiplier: 1.0), ]) } }
的
lp0sw83n3#
我认为你需要创建UIImageView与您的图像大小,并将其放置在UIScrollView(并禁用滚动)。然后您可以更改滚动视图的比例。https://www.brightec.co.uk/blog/creating-a-zoomable-image-view-in-swift
3条答案
按热度按时间z4iuyo4d1#
您可以使用此扩展:
字符串
并在您的图像中使用此函数,例如:
型
您还应该使用UIImageView属性:.contentMode,例如:
型
如果你想在你图像中增加额外的边距,你可以在你的UIImage扩展中添加这个函数:
型
4uqofj5v2#
在
UIView
中嵌入图像视图的快速示例:字符串
看起来像这样(尺寸60 x 60):
的数据
和一个示例视图控制器,将其覆盖在标签上:
型
的
lp0sw83n3#
我认为你需要创建UIImageView与您的图像大小,并将其放置在UIScrollView(并禁用滚动)。然后您可以更改滚动视图的比例。https://www.brightec.co.uk/blog/creating-a-zoomable-image-view-in-swift