UIImage imageNamed returns nil
There are only a few reasons why an image would come back nil with imageNamed:
The image is not in your bundle. Make sure the image is actually in your project. Make sure the target is checked by clicking on the file and selecting the target it belongs to.
You have the image name spelled incorrectly or a problem with the extension.
You are using a retina display but do not have an @2x image. Try changing your simulator to retina and see if it shows up.
If you are using an asset catalog make sure you have the correct devices selected in the attribute inspector.
Some tips:
If you are testing using simulator delete the app off of your simulator and clean your project, then re-run. If it still shows up it should show up on your phone (if it doesn't it's probably an issue with the case of the filename or the @2x version).
If you are testing on your phone and it doesn't show up, make sure you are using the same version of the simulator (if you have an iPhone 4/4s make sure to use the 4/4s simulator with retina).
One last thing according to this post: JPG image doesn't load with UIImage imageNamed There is some issue with certain JPG types working with imageNamed and no extension. IMO, you should be using PNGs anyway since iOS compresses them for you, unless you just have to use JPG.
UIImage imageNamed returns nil except for the first image
The image names in the screenshot appear to be different than the ones you are using in the code! In the code it has a "277" in the end where as in the image name it is a "227"
UIImage imageNamed: name returns nil the second time
Well, as you said that it's for learning purposes I've tried your code and the only real problem I found is the one pointed by @rmaddy. I don't know where your image is stored exactly but I tried it adding 4 images as Assets and changed your code from this:
NSString *basePath = @"/Users/johndoe/Desktop/soa/SoA/SoA/Models/firefly/";
// Load images
for (NSInteger i = 0; i < 4; i++) {
NSMutableString *fullPath = [NSMutableString stringWithString:basePath];
[fullPath appendFormat:@"SMALL_0000_Capa-%ld.png", i + 1];
[imageNames addObject:fullPath];
}
to this:
// Load images
for (NSInteger i = 0; i < 4; i++) {
NSString *path = [NSString stringWithFormat:@"pos%i", i];
[imageNames addObject:path];
}
and I think it worked as you expected.
iOS - UIImage imageNamed: returns null on device
I did have such a problem recently too.
After playing with filenames and paths sometimes it helps when you clean and rebuild your project.
Swift UIImage Named is nil inside UITableViewController
I'm surprised that this is working for any images, because UIImage(named:)
does not work for images inside a subdirectory of your main bundle. It only looks at the top level of the main bundle.
However, the solution is simple: don't use UIImage(named:)
. Don't use pathnames either. There's absolutely no need for that. You had the solution in your hand: contentsOfDirectory(at:)
returns an array of URLs, so when you say for item in contents
, every single item
is the URL of one of your images. So you can just load an item
directly. Call Data(contentsOf:item)
to fetch the file data, and turn that into an image with UIImage(data:)
.
How to display .svg image using swift
Try this code
var path: String = NSBundle.mainBundle().pathForResource("nameOfFile", ofType: "svg")!
var url: NSURL = NSURL.fileURLWithPath(path) //Creating a URL which points towards our path
//Creating a page request which will load our URL (Which points to our path)
var request: NSURLRequest = NSURLRequest(URL: url)
webView.loadRequest(request) //Telling our webView to load our above request
Related Topics
Storing Asynchronous Cloud Firestore Query Results in Swift
Animate Uilabel Text Between Two Numbers
"Do Not Embed", "Embed & Sign", "Embed Without Signing". What Are They. What They Do
Does @Synchronized Guarantees for Thread Safety or Not
Itms-90535 Unable to Publish iOS App with Latest Google Signin Sdk
Change the iOS Keyboard Layout to Emoji
View with Continuous Scroll; Both Horizontal and Vertical
Uitableview with Multiple Sections Using Realm and Swift
Ios 8.1 Simulator Always Uses Us Keyboard Layout Despite German Hardware Keyboard
Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5
How to Put the Image on the Right Side of the Text in a Uibutton
Keeping a Uibutton Selected After a Touch
How to Check If the iOS Device Is Locked/Unlocked