Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use launch image set on UIImageView

I am creating instruction screen for my app where I have to place a full screen sized image. For that purpose I have to consider all screen sizes and thus I have created a Launch image set in AssetCatalog.
On the view, I have UIImageView. On setting the image for the UIImageView, I get a white screen. I am guessing launch image can't be set on UIImageView.
What might be the best approach here ?
Note : Please share your answers considering the latest naming conventions (including iPhone X) if required.

like image 960
Nitish Avatar asked Jan 25 '17 07:01

Nitish


3 Answers

The launch screen is just like a regular storyboard. You can add an image view controller, text labels, or other UIKit controls.

I'd recommend against using a static image for the launch screen for a variety of reasons.

  • It can't be localized.
  • It may distort based on the screen resolution and orientation of the device.

Instead, treat the launch screen as any other view controller. Add controls, images, and static text along with the appropriate layout constraints. This approach will give you a lot more flexibility and scale better.

If you really want to add the static image, add a UIImageView control to the launch view controller and set its dimensions to the extents of the view. Then, assign the image to the image view control.

like image 177
Jeremy J Avatar answered Oct 19 '22 13:10

Jeremy J


You can not directly use the Launch Images. I found a workaround:

func getLaunchImageName() -> String? {
    // Get All PNG Images from the App Bundle
    let allPngImageNames = Bundle.main.paths(forResourcesOfType: "png", inDirectory: nil)
    // The Launch Images have a naming convention and it has a prefix 'LaunchImage'
    let expression = "SELF contains '\("LaunchImage")'"
    // Filter the Array to get the Images with the prefix 'LaunchImage'
    let res = (allPngImageNames as NSArray).filtered(using: NSPredicate(format: expression))
    var launchImageName: String = ""
    // Now you have to find the image for the Current Device and Orientation
    for launchImage in res {
        do {
            if let img = UIImage(named: (launchImage as? String ?? "")) {
                // Has image same scale and dimensions as our current device's screen?
                if img.scale == UIScreen.main.scale && (img.size.equalTo(UIScreen.main.bounds.size)) {
                    // The image with the Current Screen Resolution
                    launchImageName = launchImage as? String ?? ""
                    // You can store this image name somewhere, I have stored it in UserDefaults to use in the app anywhere
                    UserDefaults.standard.set(launchImageName, forKey: "launchImageName")
                    break
                }
            }
        }
    }
    return launchImageName
}
like image 44
hardik parmar Avatar answered Oct 19 '22 13:10

hardik parmar


I think you created a launch screen and use imageview in it.

If you use it then you must put a image in main bundle not in Assets because assets will not call on launch time. use mainbundle image.

Hope it will help you.

like image 2
Rakesh Patel Avatar answered Oct 19 '22 14:10

Rakesh Patel