没有加载在设备上的背景图像

时间:2016-10-11 11:40:02

标签: ios iphone swift uiimage

我有这个代码将图像放在背景中并应用模糊效果:

 let effect = UIBlurEffect(style: .Dark)
    override func viewDidLoad() {
         let backgroundView = UIView(frame: view.bounds)
    backgroundView.autoresizingMask = resizingMask
    backgroundView.addSubview(self.buildImageView())
    backgroundView.addSubview(self.buildBlurView())
    tableView.backgroundColor = UIColor.clearColor()
    tableView.backgroundView = backgroundView
    tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)


}
func buildImageView() -> UIImageView {
    let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
    imageView.frame = view.bounds
    imageView.autoresizingMask = resizingMask
    return imageView
}

func buildBlurView() -> UIVisualEffectView {
    let blurView = UIVisualEffectView(effect: effect)
    blurView.frame = view.bounds
    blurView.autoresizingMask = resizingMask
    return blurView
}

当我在模拟器中运行它时,它加载正常。但是当我在iPhone 6上测试时,它只会加载黑色背景。

如何解决这个问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试下面在Xcode 8中测试过的代码。代码测试为UIView ..

  **Answer 1:** From your code
   // Change bLurView Style to .dark or .extraLight If you want 
    let effect = UIBlurEffect(style: .light)

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let backgroundView = UIView(frame: view.bounds)
    // backgroundView.autoresizingMask = resizingMask
    view.addSubview(self.buildImageView())
    view.addSubview(self.buildBlurView())
    /////Code tested in UIView not in tableView.It won't do any difference just let you know///////
    tableView.backgroundColor = UIColor.clearColor()
    // tableView.backgroundView = backgroundView
    tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
    /////////////////////////////////////////////////////////////////////  
    }

    func buildImageView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
        imageView.frame = view.bounds
       // imageView.autoresizingMask = resizingMask
        return imageView
    }

    func buildBlurView() -> UIVisualEffectView {
        let blurView = UIVisualEffectView(effect: effect)
        blurView.frame = view.bounds
     //   blurView.autoresizingMask = resizingMask
        return blurView
    }

回答2:

    override func viewDidLoad() {
    super.viewDidLoad()
    // Add a background view to the table view
    let backgroundImage = UIImage(named: "your image file")
    let imageView = UIImageView(image: backgroundImage)
    self.tableView.backgroundView = imageView
    imageView.contentMode = .ScaleAspectFit 


    // no lines where there aren't cells
    tableView.tableFooterView = UIView(frame: CGRectZero)

    //set background colour to light color or clear color to get a transparent look
    tableView.backgroundColor = .lightGrayColor()

     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
     let blurView = UIVisualEffectView(effect: blurEffect)
     blurView.frame = imageView.bounds
     imageView.addSubview(blurView)

     }

    If we want to make the table view cells totally transparent you can just  set their background color to clear:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   cell.backgroundColor =  UIColor(white: 1, alpha: 0.5) // UIcolor.clear
   }

输出以上代码如下....

enter image description here