Can't load a new image from .xcassets file - swift

I'm unable to load an image that I added to a .xcassets file today.
let image = UIImage(named: "slider_star")
print(image) // nil
Other older images from the exact same assets file load just fine (in the exact same spot in code). I can see the image just fine in .xib files, and if I put it in a UIImage in a .xib file it appears just fine in the simulator, too.
I've tried cleaning my build folder, restarting Xcode, using different simulators, and loading it as an Image Literal (can select it from the image list, but simulator crashes when it tries to render the image). I also made sure the target membership is correct, and that the assets file is in copy bundle resources, and that the image files are indeed inside the assets file in Finder. Also all settings for the image set are the same as other working image sets.
Any idea why this is happening?
edit: More things I've tried from suggestions in the comments:
change name from "star" -> "slider_star"
check that the color space of all images are the same as working ones
check that there is no extra white space at the end of slider_star.
also tried making another new image set, and re-using some of the confirmed working images in it, but this also comes back nil. I think this probably rules out any issue with the image files themselves, at least. It's almost like xcode isn't seeing the updates to the .xcassets file, but that's strange because the updates are showing up and working fine in .xib files.

Finally figured this out. This is a fairly large project with multiple targets, and it seems that the code in the target I'm working in is for some reason looking at the .xcassets file from another target (I checked the other xcasset file's target membership, and it's not included in the target I'm working in, so I don't understand why this is happening).
I can work around this by putting my images in the other xcasset file (don't really like this), or specifying the bundle with UIImage(named:in:with:).
If anyone knows why this is happening, I'd love to fix the root issue (maybe it's not an issue, and I'm just not understanding how it's supposed to work). I'll let this sit for 24 hours before accepting this as an answer.

Related

How Do You Flush Cached Images on iOS Emulators

In one part of my app, I have four images, and I noticed that only two of them were displaying correctly on iOS, but all four worked on Android. After digging around, I noticed that the file extensions on the two that weren't displaying were uppercase, and I think all images need to be all lower case. So I changed the filenames in the project to all lower case and redeployed. Didn't work. Tried a few variations that didn't work, and finally made a copy of the images with a lower case extension and it worked.
Now I've made some changes to the images and copied them over the old ones in the project file. When I deploy it though, the app on iOS shows the old image. Both of these problems seem as though they could be resolved if I could flush out the cache or whatever is holding these images in the iOS emulators. Is there a way to do that?

Images disappear after app released to appstore

There is something very weird in my app after it has been released to the appstore.
Some of the UIImages i used are just missing and seen black from some reasons.
I have checked many times before in device and simulator and it appeared just fine.
Some notes:
I did drag the files and check COPY - the images are in the library for sure
Images DO appears in Copy Bundle Resources under Build Phases
I used these 2 lines in order to get the image:
[UIImage imageNamed:#"email_icon.png"] and [UIImage
imageNamed:#"facebook_icon"]
And i have just noticed that the extensions of the images are PNG not png - maybe is that the reason?
Appreciate your assistance!
Can you replicate this issue on your device? If not I'd recommend deleting the current version from your device and redeploying. You may have cached images on your device which are not allowing you from seeing the issue.
Secondly, yes, filenames on the device are case sensitive. Also make sure that you are properly naming the files and it is not simply a spelling mistake or change from an image name.
Per your line of code you are missing the file extension on the second image
[UIImage imageNamed:#"email_icon.png"] and [UIImage imageNamed:#"facebook_icon"] <-- what type of file is facebook_icon?

Could not load image Default.png

This particular stuff is getting me crazy :
Win [1365:707] Could not load the "Default.png" image referenced from a nib in the bundle with identifier "com.abc.acd"
How should I fix it and the error is only on device and not on simulator?
Open the XIB in question and make sure that none of your UIImageView's have their image property set to Default.png
Or, if you require the Default.png to be there, make sure that it exists in your Project files.
Just delete the app from the iphone and then clear all the target from the xcode.
Then run the application again
One possible reason is that the simulator finds the file because your Mac is formatted with case insensitive filesystem. That means that "Default.png", "default.png" and "DefAuLt.png" all counts as the same file.
However on the iPhone the filesystem is case sensitive which means that "Default.png", "default.png" and "DefAuLt.png" all counts as different files.
Therefore, be sure that your file actually is named "Default.png" and not "default.png" or something else.
You are getting this error because one of your NIBs refers to the image, and (for some reason) it isn't being packaged into your bundle correctly.
If you renamed your launch image (Default.png) to something different (eg MyAppBackground.png), then you'll have to find which NIB is referring to the original name and edit it to refer to the new name.
Capitalization counts. Make sure you have named the image Default.png (not default.png). The device is case-sensitive, but your Mac is not. If it works in the simulator but not on device, you probably have a capitalization problem.
The file must be included in the current build target. Select the image in your project hierarchy (Groups & Files), select "Get Info", and make sure that it's checked for the build target.
You might need to Clean (from the Build menu) the project and rebuild. Sometimes XCode just gets confused, especially if you've been moving or renaming images. Quitting & restarting XCode might help, too.
Check for duplicates of the app on the device. If you've changed the app's bundle ID, you might have this problem.

old images show after replacing with new ones in ios app

I am dealing with this really really annoying bug in X-Code where it seems to save images to some type of memory that seems impossible to clear.
I have replaced a bunch of images in which I am creating a clock animation however when I try to play the animation it showes all the old images, I have tried cleaning the app and still they continue to show even though I have deleted the images.. So then I tried changing the name of the images along with the code where they were getting called and now I'm getting a bunch more errors, So I'm just woundering if anyone knows how to get around this problem of xcode caching images.
You need to clean the build or delete it from the device/simulator. That usually takes care of it if you're sure the files are named correctly. Also make sure that you're using the correct case in your filenames, that messed me up a few times. :)
In the XCode explorer, right click on the image, use "Show in finder" and make sure they refer to the images you want.
Try using the Clean option from the Product menu in XCode.

Images and Caching - same image after name change

I've got a mysterious problem with images and caching. In my project I've got 20 photos that elegantly fade in and out with a random function that changes up the order a bit.
I found that I didn't like one image in particular, image_1.png and wanted to push it back in the rotation (each time the app starts the images start with image_1.png). So, I swapped the name of image_1.png with that of image_16.png. When I look at image in the project folder and in Resources indeed the images are as they should be but when I run the app its the same old image that starts things out.
I have reset the sim, deleted the app from my iPhone, removed the images from the project and reimported them and still the same image.
Even odder.. I have an image_0.png that is no longer in the project file at all ... its not even on my hard drive.... but it shows up as well.
Do I have a ghost in my Mac or is this just seriously persistent cache?
Clean All Targets
Sounds like a persistent cache.
Let me guess: you have your images in a directory that you add to your project as a reference?
Yeah, that stuff's weird and buggy. Even in the latest xcode. I find it helps to recreate the folder with the new contents in a different place, then remove the old one from the project and add the new one. Perhaps even closing the project before re-adding.
XCode sucks.