Can't add UIImage to UIImageView (Swift) - ios

I am trying to add a UIImage to a UIImageView in Swift as of March 2019. The image is titled correctly and is saved in the iCloud Drive in the same directory as the Swift file. I am able to add the UIImageView to a view controller perfectly fine but the image does not show up in it. I believe I have the right file name as I copied it directly from the finder. What I want to do is have an image inside of a viewer which is scaled by aspect ratio to fit the viewer.
let discoverIcon = UIImage(named: "DiscoverButtonIcon.png")
let discoverImageView = UIImageView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
discoverImageView.image = discoverIcon
discoverImageView.contentMode = .scaleAspectFit
view.addSubview(discoverImageView)
This is what I have tried to do but the UIImage will not show up.

Temporarily change
UIImage(named: "DiscoverButtonIcon.png")
To
UIImage(named: "DiscoverButtonIcon.png")!
Run the app. If it crashes, you have the wrong name or the image is not in your app bundle at all.
is saved in the iCloud Drive in the same directory as the Swift file
That sounds like the problem. That is not where the image needs to be. It needs to be in your asset catalog or app bundle.

The best practice would be adding the image to assets folder. By adding the image your assets.xcassets it will be added to your Bundle Resources.
If you don't want to add it to the assets.xcassets folder, add the image as another item in your project. However, make sure you add the image to the right target. (You can check in File Inspector > Target Membership)
You can check Build Phases > Copy Bundle Resources to make sure that you add the image to the Bundle correctly.
// If you add image to assets.xcassets
let image = UIImage(named: "DiscoverButtonIcon")
// If you add image as a resource
let image = UIImage(named: "DiscoverButtonIcon.png")
// Set a break point here to debug your image.
Related debugging post: Xcode debugging - displaying images

Try to add your image to Images.xcassets.

Related

Adding image resource to iOS app and access it

I have an Xcode project which needs to access an image. My questions are:
How to add images to the Xcode project. Do I need to follow a specific structure or convention?
How do I access the image I added in step (a)
Online links say add bundle resources but none of them gives a conclusive answer showing what to add in Xcode project and how to access the same.
The best way to save image in your folder by creating an Asset Folder.
File -> New File -> Asset Catalog
Name the asset as image, it will create an asset folder image.xcassets
In this asset catalog, you will keep your images in specific folder.
you have to just write
imageView.image = UIImage(named : "MY_IMAGE_NAME")
When you create a project,there would be a folder named Assets.xcassets.You can drag the image you need into the Assets.xcassets.
And when you need to show the image,code this:
//the image name is `image.jpg`
let image = UIImage(named : "image")
imageView.image = image
PS:
You can also drag the image to the project directly,and the code:
//the image name is `image.jpg`
let image = UIImage(named : "image.jpg")
imageView.image = image
You will be able to find a folder named Assets.xcassets in your project hierarchy.
Drag and drop the image you wish to add into this folder.Here you can select 1X 2X or 3X size for your image.
After doing this you will have to create an UIImage object to access it.
for example:
let image = UIImage(named:"image_name")
Now you can use this image object wherever you wish to use the image.
Whenever you are creating a project, you getting a folder Assets.xcassets. In this just add a New Image Set, add the name which one is your image name and drop here 1x, 2x & 3x here.
OR you can create a separate Asset Cataolog.

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.
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.

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.

Xcode, images won't load into UIImageView

I just updated to Xcode 7.3, not sure if that's related. I'm attempting to display png or jpeg images in a UIImageView using code. The images I had installed before updating appear fine. The new images show nil when I query the unsuccessful attempt to load. The images appear fine if I load them into the Asset catalog but there are three reasons I'd rather avoid doing that.
BTW, am using UIImage(named: _) to attempt to load.
Edit: It appears the issue is restricted to JPEGs. The initial PNG I tried, which I've used for years in a nib view, wouldn't load but other PNG's I just created seem to load OK. Since almost all my images will be PNG's I'm good to go but still curious.
As long as you've put your image in the project and can see it in you navigation area, you should be able to use it like this.
var imageView: UIImageView!
var yourImage: UIImage!
func yourFunction() {
self.imageView.frame = CGRectMake(0, 0, 50, 50)
self.yourImage = UIImage(named: "yourImageName.jpg")
self.imageView.image = self.yourImage
}

iOS App Issue - Can't access image from image asset catalog

I'm new to iOS development, so apologies for advance. I know this has been covered earlier but I can't seem to find anything that helps with my issue.
Its a fairly simple but annoying issue.
I have a image in assets with a file name of hollwood.png but a xcode name, "hollywood" (no png).
I am trying to set a UIImageView to display it.
So in ViewDidLoad I have the following: (imageContainer is the UIImageView IBOutlet)
- (void)viewDidLoad {
super viewDidLoad];
self.model = [Model sharedModel];
self.imageContainer.image = [UIImage imageNamed:#"hollywood"];
}
I can not get it to display the image from Assets.
I know the UIImageView outlet works correctly because I can have the app choose a photo from gallery and set the photo to UIImageView.
I'm having a very difficult time figuring out how to load the image I saved in the assets catalog into UIImageView.
I just had this issue. I had dragged the image and for some reason Xcode added a weird "Design" locale and I couldn't load the image using UIImage(named: ). I don't know if this is your case, but you can try right clicking on the image in the Asset Catalog, "Show in Finder", opening the folder and checking the "Contents.json" file. Each image there should only have the keys "idiom", "filename" and "scale".
Make sure your image is added in Compile Sources
AppTarget->Build Phases-> Compile Sources
If not try to add that and check.

Resources