【问题标题】:How do you move the uiimage inside the uiimageview to the right while retaining the aspect ratio of the original image?如何在保留原始图像纵横比的同时将 uiimageview 内的 uiimage 向右移动?
【发布时间】:2020-12-11 02:33:05
【问题描述】:

当用户使用 uiimagepicker 导入图像时,我想将图像向右移动,但是当我设置内容模式 = .right 时,会发生这种情况:The image enlarges for some reason and it looks like it moves to the left

有什么办法可以保持uiimageview的纵横比和导入图片的纵横比,同时在image view里面也向右移动。

This is how I want it to be

【问题讨论】:

  • UIImageView 没有“aspectFIt 和对齐”功能。不过,您可以通过多种方式完成此操作...一种是使用带有图像视图的子类视图作为子视图...搜索uiimageview aspect fit align 以获得大量结果。这个可能对你有用:github.com/sochalewski/UIImageViewAlignedSwift

标签: swift uiimageview uiimagepickercontroller


【解决方案1】:

将您的 UIImage 内容模式设置为方面填充或方面适合。然后使用自动布局。

【讨论】:

    【解决方案2】:

    这是一种方法:自定义视图,使用将内容设置为图像的子图层...

    • 添加CALayer 作为子层
    • 计算视图边界内图像的纵横比缩放矩形
    • 将图像层的框架设置为缩放的矩形
    • 然后根据所需的对齐方式设置图层的原点

    一个简单的例子:

    class AspectAlignImageView: UIView {
        
        enum AspectAlign {
            case top, left, right, bottom, center
        }
        
        // this is an array so we can set two options
        //  if, for example, we don't know if the image will be
        //  taller or narrower
        //  for example:
        //      [.top, .right] will put a
        //          wide image aligned top
        //          narrow image aligned right
        public var alignment: [AspectAlign] = [.center]
        public var image: UIImage?
        
        private let imgLayer: CALayer = CALayer()
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            // make sure we have an image
            if let img = image {
                // only add the sublayer once
                if imgLayer.superlayer == nil {
                    layer.addSublayer(imgLayer)
                }
                imgLayer.contentsGravity = .resize
                imgLayer.contents = img.cgImage
    
                // calculate the aspect-scaled rect inside our bounds
                var scaledImageRect = CGRect.zero
                let aspectWidth:CGFloat = bounds.width / img.size.width
                let aspectHeight:CGFloat = bounds.height / img.size.height
                let aspectRatio:CGFloat = min(aspectWidth, aspectHeight)
                scaledImageRect.size.width = img.size.width * aspectRatio
                scaledImageRect.size.height = img.size.height * aspectRatio
    
                // set image layer frame to aspect-scaled rect
                imgLayer.frame = scaledImageRect
                
                // align as specified
                if alignment.contains(.top) {
                    imgLayer.frame.origin.y = 0
                }
                if alignment.contains(.left) {
                    imgLayer.frame.origin.x = 0
                }
                if alignment.contains(.bottom) {
                    imgLayer.frame.origin.y = bounds.maxY - scaledImageRect.height
                }
                if alignment.contains(.right) {
                    imgLayer.frame.origin.x = bounds.maxX - scaledImageRect.width
                }
            }
        }
        
    }
    
    class TestAlignViewController: UIViewController {
    
        let testView = AspectAlignImageView()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            testView.translatesAutoresizingMaskIntoConstraints = false
            
            view.addSubview(testView)
            
            NSLayoutConstraint.activate([
                // constrain test view 240x240 square
                testView.widthAnchor.constraint(equalToConstant: 240.0),
                testView.heightAnchor.constraint(equalTo: testView.widthAnchor),
                // centered in view
                testView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                testView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            ])
            
            if let img = UIImage(named: "bottle") {
                testView.image = img
            }
            testView.alignment = [.right]
        
            // so we can see the actual view frame
            testView.backgroundColor = .green
        }
        
    }
    

    使用这张图片:

    在 240x240 视图中(视图背景设置为绿色,以便我们可以看到它的框架),我们得到以下结果:

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多