Use launch image set on UIImageView - ios

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.

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.

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
}

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.

In your LaunchVC take a full size UIImageView
Put your set of images in the in a single resource folder as Resources-> Images.
Put all images in AssetCatalog as you already done. For this you must have 1x, 2x, 3x images sizes to show images in AssetCatalog.
I don't think it is possible that you have image either in bundle or in Asset Catlog and there is no typo for image name and still you don't get image.

Without knowing the image, its hard to tell.
One way could be to simply use a SVG file (or in terms of iOS a PDF Vector file) and just use a image asset with the Scales set to single scale.
If you have a image that is not "scalable" because of text or other stuff, you might need to consider building it yourself in iOS with labels and imageViews.

I think you need new launch screen for displaying your launch image. For this you create new storyboard and change default launch screen to your new launch screen in targets. See this image
Here in Launch Screen File change select your new launch screen. This new launch screen can access your image files from assets.
In so many situations if we want to display animations in splash screen we follow this approach. Try this approach it can solve your problem.

Related

iOS dark mode - image assets not redrawn

I'm implementing the dark mode for iOS. The problem occurs with the images:
I've opened the Assets.xcassets and changed the "appearances" "to Any, Dark"
Of course I've added the new image.
Unfortunately the images are not being redrawn when overriding the environment interface style in xcode.
I've tried catching the traitCollectionDidChange method in my viewController and it is properly called. I could set the new image (origImage_dark), but shouldn't it be automatic? That's what the asset settings are made for. I'm using the .alwaysOriginal rendering of the image.
Running the app with dynamic resolve of the image helped:
let image = UIImage(named: "someImage")
let asset = image?.imageAsset
let resolvedImage = asset?.image(with: traitCollection)
After this, reverting back to the original way of setting the images seemed to work. Xcode, thanks a lot!

UIImageView: How to show scaled image?

I'm new to iOS app development and I begun to learn from this great tutorial:
Start Developing iOS Apps (Swift)
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1
Everything looks great but when trying to load an image from "Camera roll" on the simulator, I get the image shown oversized and filling all the iPhone simulator screen instead of just showing into the UIImageView box as shown on tutorial images.
The funny fact is that also downloading the correct project provided at the end of the lesson (see the bottom of the page) I get the same issue.
Googling around I get some ideas to insert a:
// The info dictionary may contain multiple representations of the image. You want to use the original
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image
photoImageView.image = selectedImage
photoImageView.contentMode = UIViewContentMode.scaleAspectFit
// Dismiss the picker
dismiss(animated: true, completion: nil)
after loading image from the camera roll, but lead to no results...
I'm using Xcode 9.2 with deployment target 11.2 and iPhone 8 plus as a target for the simulator.
Any help is apreciated.
It happens because the sample code is only restricting the proportion of the UIImageView (the 1:1 constraint you added in storyboard). In this way, it will always keep your imageView squared but will not limit the size.
As the sample tells, you entered a placeholder size, that is only kept until you set some image on it. When you enter a new image on the UIImageView it changes to the size of the image you set (in your case, probably a bigger image for the camera roll). That's probably why its getting so large.
I think the easy way to fix this to add other constraints in storyboard to limit the size (like actually size or border constraints).

Navigation bar button image coming out pixelated

I'm trying to use a UIImage for my left bar button on my navigation controller. In the design file the button is 40X40px. This ends up being too big to be used for the button. As seen below:
I reduced the button down to 16px and now I get something like this:
I can't seem to get the size right for the button to look clear. I've tried 28px-10px. Any advice?
EDIT Added 10px, 20px, and 30px of image
Code for setting button
var settingsImage = UIImage(named: "SettingsButton10px.png")
settingsImage = settingsImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let settingsButton : UIBarButtonItem = UIBarButtonItem(image: settingsImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(HomeController.settingsPressed))
self.navigationItem.leftBarButtonItem = settingsButton
In your projects Assets.xcassets, create an Image Set and provide #1x, #2x and #3x versions. So if the original image size is 20x20, you also need a 40x40, and a 60x60.
Then, when you reference the image in a UIImage, do it with the name of the Image set from the Assets.xcassets without the file extensions. So if the name of the ImageSet in Assets.xcassets was SettingsIcon, you would load the image like so:
UIImage(named: "SettingsIcon")
If adding #2x & #3x image size does't helped you why can't we go with Vector pdf..? Here is what i have got
You can get PDF images from here,Change the image formate from PNG to PDF.
Then in drag and drop the downloaded pdf image in Assets.xcassets. Then Change Scale Factors to Single Vector
3.Make sure Image set look like this
Since we added the image in Assets no need to add extension so replace this
var settingsImage = UIImage(named: "SettingsButton10px.png")
with
var settingsImage = UIImage(named: "SettingsButton10px")
RESUT:
Using PNG 25px:
Using PDF 25px:
So now no need to add double and triple sized images,Pixalation issue will be fixed when we use PDF.
Apple has some information on custom icon sizes here: https://developer.apple.com/ios/human-interface-guidelines/graphics/custom-icons/
The size you want for an iPhone 5 is "About 44px by 44px," so you've got that in the right ballpark. It's a matter of making sure iOS knows this is a retina resolution asset.
The easiest way to do this is putting it in an "Image Set" inside Assets.xcassets. These are containers for multiple different scales, and allow iOS to pick the appropriate version for a given device and display it without pixelation.
The "1x" scale was used in pre-retina devices, "2x" is most common (and is what you'll need to set for the iPhone 5 simulator), and "3x" is for iPhone 6 Plus sized phone.
If you haven't created the 3x image yet, it's fine to leave it blank for now.

Using PDFs for icon images in Xcode 7.2

I'm attempting to use PDF files as icons in an app I'm working on. The issue I'm encountering is I'm getting inconsistent tint colors.
If I set a button image from interface builder, the icon image shows up black at runtime. Every time. Regardless of what I attempt to set from interface builder.
I tried setting my button icon image via code and instead of showing up black, it's white:
let myGraphicFile = UIImage(named: "myPDFImage")
let myButtonImage = myGraphicFile?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myButton.setImage(myButtonImage, forState: .Normal)
From code, regardless of what I attempt to set the tint to, it's always white from code.
I discovered this post relating to Xcode 6.x, but I think it might be dated, as I'm able to partially do it, but I can't set the tint.
Use PDF in XCode for an AppIcon (.appiconset collection)
I create the icons in Inkscape, save as PDF 1.5. I add the file to Images.xcassets. In Images.xcassets' attributes inspector, I'm setting:
Devices to Universal
Scale factor to Single Vector.
Summary: I can get it to show up and scale properly, but it's either black from interface builder or white from code. I suspect I'm missing something re: how to save the file from Inkscape.
Thank you for reading. If you have any suggestions, I welcome them.
I have figured out how to create vector icons with Inkscape. When you use PDFs to display icons in iOS, you need to alter the Attributes Inspector for your icon in xcAssets as follows:
1) Drag the PDF into xcAssets
2) Set devices (I did Universal and it worked fine)
3) If your PDF icon is under 1x, 2x, or 3x size class, drag it to Universal and delete the rest of them.
4) Set Scale Factors to Single Vector.
5) Render as Template Image.
Once it's configured there, then you just treat it was you would any other image in interface builder. It's essentially the same thing I was doing in code, but I don't think it gets done in code...it's gotta be done on xcAssets where the image lives. It's my understanding iOS renders vector images for the size class at run time. I think by attempting to tweak it in code wasn't working because the image had already been rendered.
If anyone has any questions on this, I found this link helpful in resolving my issue.
Additionally, this post covers the topic, too. https://stackoverflow.com/a/25804358/4475605

Use high definition tab bar icons programmatically?

I was wondering how I should go about using high definition tab bar icons if my tab bar icons are being set programmatically. I'm pretty much just doing the standard:
tabBarItem2.image = [UIImage imageNamed:#"ExploreIcon.png"];
Will putting two images in my supporting files named "ExploreIcon.png" at 30x30 pixels and "ExploreIcon#2x.png" work without using additional code? Thanks for the help!
You don't need to specify the retina version, the system will use the higher resolution version if present in the bundle. Also you don't need to add the extension
[UIImage imageNamed:#"ExploreIcon"];
the two images should have te same name for example :
ExploreIcon.png
ExploreIcon#2x.png
You need not consider choosing file which fits and displays better on a specified device, Let iOS do that for you.
Just keep files to support all possible devices.
Follow:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
tabBarItem2.image = [UIImage imageNamed:#"ExploreIcon.png"];
This is good enough. Just make sure the file names are appropriate. i.e.
you have to add an imageview and set the cgrect size 40*40 and than you will add that image view on the tab bar icon image.

Resources