简体   繁体   English

如何将uiview背景和uiimageview设置为居中,缩放以填充和遮盖边界?

[英]How to set uiview background and uiimageview to be centered, scaled to fill and masked to bounds?

Hello I am working on feed and I am facing issues with displaying images correctly. 您好,我正在处理Feed,但遇到正确显示图像的问题。 The issue is with first two UITableViewCells , that get displayed after tableView loads. 问题在于前两个UITableViewCells ,它们在tableView加载后显示。 The remaining cells load correctly. 其余单元正确加载。 The code is mention below that I am using to display the images. 下面提到的代码是我用来显示图像的代码。

Following are some screenshots of the issue. 以下是该问题的一些屏幕截图。

https://ibb.co/W3rCsmB https://ibb.co/W3rCsmB

https://ibb.co/8PZKVSh https://ibb.co/8PZKVSh

https://ibb.co/jhwm6JH https://ibb.co/jhwm6JH

https://ibb.co/cLyfC3C https://ibb.co/cLyfC3C

//Code to add Image. 
//Code written in configurecell() 

let imageURL = URL(string: ForFeed.image_url)
    let media = UIImageView(frame: mediaView.bounds)
    media.downloaded(from: imageURL!)
    media.contentMode = UIViewContentMode.scaleAspectFill
    media.layer.masksToBounds = true
    self.mediaView.insertSubview(media, at: 0)

//Code to add Background image.
//Code written in awakefromnib()

UIGraphicsBeginImageContext(self.mediaView.frame.size)
    let mediaPlaceholder = UIImage(named: "nilmedia")
    mediaPlaceholder?.draw(in: self.mediaView.bounds)

    if let image = UIGraphicsGetImageFromCurrentImageContext(){
        UIGraphicsEndImageContext()
        self.mediaView.backgroundColor = UIColor(patternImage: image)
    }else{
        UIGraphicsEndImageContext()
        debugPrint("Image not available")
    }

The result should display images properly. 结果应正确显示图像。

Just headover to storyboard and select he image view and then go to Attribute inspector set contentView to aspect fill 只需移至情节提要,然后选择图像视图,然后转到“属性”检查器即可将contentView设置为长宽比填充

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM