简体   繁体   中英

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. The issue is with first two UITableViewCells , that get displayed after tableView loads. 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/8PZKVSh

https://ibb.co/jhwm6JH

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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