none of views showing image - swift

not setting image on every views like UIView , UIImageView
mainView.backgroundColor = UIColor(patternImage: UIImage(named: "headerDriver.png")!)
img.image = UIImage(named: "about")
and there is warning in console
Driver was compiled with optimization - stepping may behave oddly; variables may not be available.
i have test it with image literal to make sure image exist
i suddenly deleted the optimization level and now drop down menu is gone i can't change the value of it
there is a problem with
optimization level
in build setting i'm sure

as you mentioned if asset folder is not in compile resource.xcode will not compile it so your app crash because there is no asset folder either all you pictures
solution :
navigate your target -> build phases - > compile source -> add (+)

Related

Could not load the "" image referenced from a nib

I have no images called "" (nothing). I've gone through the .xib file of the screen where this pops up and can't find anything that would cause this. Anybody who have had the same issue? Here is the full warning:
Could not load the "" image referenced from a nib in the bundle with
identifier "com.blah.Blah"
You can do this to check it from your storyboard, or nib.
Open it as Source Code:
Then "Find" the image name in it, see if it exists but already invalid in your project, probably you have deleted or changed its name, and now it doesn't seem valid.
I had the same issue and in my case there was an UIButton with an invalid image reference in the storyboard file. The reference wasn't empty so it was not trivial to spot by just searching for "" in the storyboard file. However, Xcode did show "Unknown image" for the button in the Background field instead of "Default Background Image". In the storyboard file it appeared as
<state key="normal" title="Use" backgroundImage="0E39AEA8-7F29-40B2-96B1-63B99047E8D5">
so perhaps grepping for backgroundImage and looking for non-familiar references will help find the cause.
I had this warning because of a UITabBarItem. In the attributes inspector for the field "Selected Image" I had a file listed that was also the value for the field "Image". Nevertheless, I received warnings until I made the "Selected Image" field blank.
Just for reference, this problem is also caused by using .jpg images and referencing them inside of storyboards/nibs. iOS 8 appears to be able to handle the proper type checking between jpg and png images at runtime. However, on iOS 7 and below you will need to manually set the image in the code and remove the image reference inside of the storyboard to get rid of the warning.
Remember you need to manually set the .jpg when referencing jpg images.
[self.imageView setImage:[UIImage imageNamed:#"yourImage.jpg"]];
I came looking for the answer. I searched my entire project and storyboard source code for "" and nowhere could I find it. The clue that the view that is displayed when the error appears is the one you need.
Open your xib or storyboard selecting the relevant viewController.
On the left click all grey arrows to expand the selection in your
view
look for the UIImageView icon (its the one with the palm tree)
select it and open the "Attributes inspector" on the right hand side.
In the ImageView section at the top in either or both Image and
Highlighted you will see greyed out "unknown image".
Use the drop down menu to change this from "unknown image" and build
run
voila
The nib (or ultimately the xib) that is the cause of the warning message is the one that is loaded when the message gets written. You then can look at the controls that use images in the inspector and find one where the image name is "Unknown image". You can then clear this value by setting it to some value and then clearing out the value. Note that with UIButton you'll need to try the four different values of "State Config" (Default, Highlighted, Selected & Disabled) to see where the invalid reference might be hiding.
In my case it was Unknown image in UIBarButtonItem in storyboard.
Very simple remove the image that is not displaying from you project and then clean the project by going to Product--> clean then once again put the image into your project and done. this worked for me.
In xcode 5.1 I had this same issue. The only way I could finally get it to work when using storyboard was to add the images as an image asset catalog. From what I read maybe they changed something in 5.1. I thought that storyboards don't have nibs - so it would make sense that asset catalog replaces the old method.
Project > General > App Icon Arrow > Import New Folder.
As soon as I did that the NIB references went away and my images showed up.
I'm using Asset Catalog on Xcode 6. Simply removing and re-adding the Asset Catalog fix the problem
in my situation, in my xib file,I had ever setup image name for some UIButton and UIImageView ,and delete these image name after sometime, but for unknown reason, these image still apear in the Interface Builder Editer, finally I reset these image name with any image name and delete them again,now problem resolved.
This worked for me:
1- Quit Xcode
2- Make a copy of your .storyboard for backup.
3- Open your .storyboard in a text editor and delete image nodes and image node attributes with empty references.
delete node like this --> <image name=" " .../>
delete image attributes like this --> <tabBarItem key="tabBarItem" title="untitled" image=" " id="6"/>
4- Save the file
5- Open your project again in Xcode.
6- Run Product->Clean
7- Run your project
Warning should be gone.
I had a very similiar problem which I eventually found. The line below was causing the problem using the open as source code method mentioned above.
<tabBarItem key="tabBarItem" title="Events"
image="319365D1-7058-46CD-8420-18E0EAFB2F29" id="a5a-IF-j9f"/>
I got this error from an undo action, simply dropping the image back onto the view controller solved the problem.
Atb
In my case it was because one of my button images was wrongly added to the Background Image container instead of the Image container (in the Inspector). I found it by going through the entire storyboard hierarchy and checking each image and button setting.
Please do a search for image= shows various results. Select results from nibs or storyboard open it, if there is some value like image="2342D4DF-1E6B-42B8-847A-F9F622921D02" delete it and enjoy.
i also faced same problem because i edited image in photoshop and saved as a psd format make sure you save your edited images in jpg or png format in my case that was the problem hope it helps!!!
I got this problem for all of my images after switching one of the label's text to 'attributed' and using one of the 'Fun' fonts. Even though I switch it back to 'plain' I still get the "Could not load..." error - until I re-load and re-reference all of my images again.
Initially my image name is "bg" When i found issue, i rename image as "background" but when click on image and hit "Show in Finder"changes was not reflected. image was name as "bg-1.jpg".
so i removed from project convert image to png and drag and drop into
project it works for me.
Believe or not, sometimes this happens because some IBOutlet was removed and in the storyboard have the reference to it.
Make sure the 'Target Membership' for the 'Assets.xcassets' has been selected.
Click 'Assets.xcassets'
In the 'File inspector', make sure 'Target Membership' selected.

UIImage imageNamed requires pathForResource?

How necessary is it to search for a path to an image using the NSBundle method pathForResource when creating a UIImage using imageNamed? I see tutorial codes that just specifies the name of the image directly, and then code that goes the extra mile to find the path first.
In my experience, I've always just used the name directly and it's always worked fine. I assumed that it automatically knew how to find the image. How important or under what circumstances would it be necessary to do more than this?
Not at all ... is the answer to the original question:
How necessary is it to search for a path to an image using the NSBundle method pathForResource when creating a UIImage using imageNamed?
Not much .. is how correct the accepted answer from Zoul and the other one from Ranga are. To be fair: they are correct if you're talking about the application bundle directory structure, or for the (rare) case where the image is in a "blue" folder in Xcode (more on that later), but not for the most common cases
Anyway, on to the one true answer.
As usual I found this question while trying to find the answer myself. I never found the documentation or the other answers to this question satisfactory, so I decided to test.
My test details are all below, but let me summarize the results here.
In short, when using imageNamed: to load your images, it depends where you put them:
if your images are in the root of your project, even if organized in a purely logical Xcode group, then no, you don't
need to think about the path: just the image name.
if your images are in a group that is attached to a directory in your file system, via "create groups for added folders" then you still don't need to worry about the name.
if your images are in a "blue" group, that is attached to a directory in your file system via "create folder references for added folders", then you can load it with imageNamed: by specifying a relative path as suggested (coincidentally?) by the accepted answer above.
if you use the main alternative to imageNamed:, imageWithContentsOfFile:, you do indeed need the full path to the file including the bundle path, which means you need to know how the Xcode navigator structure translates into paths in your bundle directory structure.
The other important differences between these two methods:
imageNamed doesn't require that you specify the filetype extension,
so merely "icon" not "icon.png", whereas imageWithContentsOfFile does
require the full file name
this first point helps with the second feature: imageNamed will
automatically load the retina version of an image if there is one, by adding #2x to your file name. So if you ask for "icon", on a
retina display it will try to load "icon#2x.png".
imageWithContentsOfFile does not
imageNamed caches the image: and therein lies a lot of the
controversy around it: If you search SO or the web at large you'll
find a lot of posts recommending you avoid it because it doesn't
clear its cache properly. This, however, was fixed years ago, so you
don't need to worry about it failing to clear its cache. You do still
need to worry about the fact that it caches at all, though. If your
images are big and are not loaded very often, you'll conserve
memory by loading them from file and not caching them. This is
nothing to do with leaks: even if you don't have leaks, you still
have limited memory on the device, and you don't want to cache
unnecessarily. It's the classic caching tradeoff: what's more
important in your situation? Memory performance or cpu performance
(time).
So on with my tests.
What I did was create a simple UITableView app with 3 simple icon files, shown in the rows of the table using different methods. The icons are different in their location in the Xcode project structure. Note the emphasis on Xcode. The key to understanding the answer to the original question is that there are three totally different project directory structures in an iOS app: There's the one you see in the Xcode navigator, the one on the file system for the same project that you see in Finder (right click any item in the Xcode navigator and select "show in Finder") and, the one you seldom see, the "bundle" directory structure of the deployed app. You can see this last one in Finder too - by finding your app in ~/Library/Application Support/iPhone Simulator, and drilling down into the .app directory. I'll show you a picture of mine in a minute.
So in my app, I dragged all three icon png image files into Xcode in different ways:
icon1.png (a clock), I dragged in as a file to the root of the Xcode project,
then I later created a new group in Xcode and dragged it into
that. This group is not represented by any directory in the file
system: it's a pure Xcode group. Hence it's name: "JustGroup"
icon2.png (an eye), I originally put my file system in directory called
"RealDir", and I dragged this entire directory into Xcode, and when
asked, I chose the "Create groups for any added folders" option.
This means that the RealDir group in Xcode is attached to a real
directory called RealDir in the filesystem (in my project directory)
and that icon2.png is in there.
icon3.png (a target), I also had in a separate directory, which I also dragged
into Xcode. Only this time I chose the 2nd radio option "Create
folder references for any added folders". This creates a so-called
"blue" group in Xcode. More on what this is all about later. I
called this group (and directory) "FolderReference"
Here's a shot of the choice that Xcode gives you:
And here's what my project structure looks like in Xcode:
Now, in my app, I used two methods for loading each of the icons: UIImage imageNamed: and UIImage imageWithContentsOfFile. I created a bunch of rows in my table with the title of the each cell being the name of the group containing the icon: JustGroup, RealDir or FolderReference, plus the name of the method used: imageNamed vs fromFile (which I'm using as abbreviation of imageWithContentsOfFile)
The detail label of the cell (the fainter text under the title) shows the file or path name I gave to the method.
To be clear, in the case of "fromFile", I'm adding the bundle path to the "relative" name you see.
So for "fromFile", I'm actually using this code:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [NSString stringWithFormat:#"%#/%#", bundlePath, filePath];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
where "filePath" is the path you see in the table cell detail label.
For imageNamed:, on the other hand, the filePath in the cell detail is passed verbatim.
And the row image is, naturally, the image that is loaded. So for the rows in the table that have no image, the image loading failed.
Here, in a nutshell, are the results. If you read nothing of this post, at least a glance at this image will tell you all you need to know.
Here's the basic explanation in easily digestible points:
as it states in the official documentation, the imageNamed: method loads images from the application bundle. This means you don't need to specify the bundle location, only the file name. And even then, just the base name of the file. The documentation is a little thin here, it should really make it clear that it loads the image from the given file path relative to the application bundle root directory.
(here's the kicker, pay attention to this one) that rule about the bundle directory, refers the root directory in your the bundle of your deployed app. If you go exploring, that means in the ".app" directory itself. That's not the same as the root directory of the Xcode project in the Xcode navigator, nor is it the same as the root directory of the Xcode project in finder
this is because, when deploying your app to the device (or simulator) all project directories represented by "groups for added folders" are flattened. That is, the directory is ignored, and all of its contents dumped unceremoniously into the root directory of the bundle. (I say "unceremoniously" because if there are files with the same name in different folders, they'll collide here and you'll get no help in resolving the problems that causes.) This is the case of RealDir in my example: in the deployed app, RealDir no longer exists, and icon2.png is left to mix in with the general population (scary). It goes almost without saying that "JustGroup", the purely logical Xcode group, is also ignored - it was never a real directory anyway, just a visual aid to the Xcode user - and icon1.png is also in the bundle root.
This is why imageNamed: was able to load icon2.
And also why imageWithContentsOfFile was not able to find it in "RealDir/image2.png": because there is no RealDir directory in the deployed app.
"blue folders", on the other hand, that is, directories represented by "folder references for added folders", are in fact retained in the app bundle directory structure. This, apparently is the point of blue folders: they give you a way to create a directory structure in your deployed app. I'm not sure of the original raison d'etre for this, but one good use case is where you have several directories containing alternative versions of resource files with the same name and you want your app to be able to switch between them at runtime by changing directory. Anyway, the icon3.png in my FolderReference, remained in my FolderReference directory in the deployed app.
This is why imageNamed: couldn't find it with "icon3", but could find it with "FolderReference/icon3"
imageWithContentsOfFile was able to find it also using FolderReference, but only when attached, remember to the full bundle path using the code above. (Key difference here: imageNamed works with a relative path in this case, imageWithContentsOfFile always works with an absolute path).
To clarify, here are my folder structures:
You saw my Xcode project navigator structure above, here is the file system directory underneath it:
And finally, perhaps most importantly, the deployed bundle file system directory structure:
Note: I found this at this location on my Mac: you will find yours in a similar location - you might have to search a bit to find which ugly-GUID-named subdirectory contains your app.
~/Library/Application Support/iPhone Simulator/5.1/Applications/4EB386B2-CD7E-4590-9757-18DDDEE6AF4F/ImageLoadingTest.app
I hope this helps. Testing, exploring and finally, describing it certainly helped me.
The docs say that “the method looks for an image with the specified name in the application’s main bundle”, so I’d say you can always use just the name. The only exception might be images stored inside subfolders, especially when you have foo/image.png and bar/image.png. I don’t know whether [UIImage imageNamed:#"foo/image"] would work, but it’s trivial to try.
(What’s a bit confusing in these cases is that the groups in the Xcode tree do not correspond to folders in the resulting app bundle. Their contents are smashed together to the root of the bundle, unless you use a blue folder reference instead of a regular group.)
I created a new Xcode project (single view, AppDelelgate, ViewController class, storyboard etc ).
Created an Images group.
Used Paintbrush to create a 16x16 png file Wall1.png and dropped it into the Images group in Xcode (let Xcode copy the files).
In the ViewController viewDidLoad method added the code:
UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 16, 16)];
UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"Images/Wall1" ofType:#"png"]];
imageView.image = image;
UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 16, 16)];
UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"Wall1" ofType:#"png"]];
imageView.image = image;
[self.view addSubview:imageView];
Ran the app on my phone, the image would not appear
Added a break point at [self.view addSubview:imageView];
image was null
Opened terminal and changed directory to my project, Wall1.png was not a group folder Images. Removed the png from the project, created a Images folder, moved Wall1.png into the folder. Added the existing file Wall1.png to the group Images.
Ran the app, the image still doesn't appear.
image was null
Changed Images/Wall1 to Wall1
Ran the app, boom image is displayed1
If you create a group for your images, Xcode does not create a respective directory. Create one manually if you want to (I prefer to keep my images in a seperate folder). Dont specify the full path to your image file when using UIImage imageWithContentsOfFile.
Try this.
[UIImage imageNamed:#"your directory path of image"]
[UIImage imageNamed:#"Dir1/folder1/folder2/imagename.jpeg"]

iOS: How to use images in custom bundle in Interface Builder?

I have a bundle where i put images in it.
The contents are:
MyBundle.bundle/images/picture1.png
MyBundle.bundle/images/picture2.png
I dragged MyBundle.bundle into my project.
Now I can see these images in Interface Builder and can even use them. However, when I run the app, I don't see the images.
What's wrong?
Thanks,
Make sure the bundle is getting copied over in the "Build Phases" of the project settings for the target.
Also, try setting them progrmatically and see if they show:
myImageView.image = [UIImage imageNamed:#"MyBundle.bundle/images/picture1.png"];
I just tried this in my project and found that what you need to do is specify the image with the bundle name in front like this:
In the IB the image will look broken:
but when you build and run your project the image will show correctly.
So, in your case, use MyBundle.bundle/images/picture1.png in the actual Interface Builder Image box.
Also, when you dont put the bundle name in front, you get this warning when you load the view with the bundle image:
2011-10-12 08:22:16.360 UTDebug[721:11c03] Could not load the "map.png" image referenced from a nib in the bundle with identifier "com.companyname.myproject"
You need to put the XIB in the bundle, too. When you do that, resources for the interface will be relative to the bundle, so they'll work both in IB and when you load the interface in your app.
You might have a stray build setting from having created the bundle as a Mac OS X target.
See my other answer for the solution.

Images in iPhone app appear in Simulator, but not when compiled to device

I am learning iPhone development, and have built a simple app that has an image that changes to another image when you tap it.
If i build it to the simulator, it works fine, but if i build it to the device the images dont appear. i feel like they aren't being copied across. I have checked they have been named correctly and i have made sure i imported them to 'resources'
I dont think it is a problem with the code because i added a thing to also make text appear when the image is tapped, and this works, so a button is still there doing something, it just doesn't have an image on it.
-(IBAction)changeImage:(id)sender {
[fortuneCookieButton setImage:[UIImage imageNamed:#"Image2.jpg"] forState:UIControlStateNormal];
label.hidden = NO;
}
-(IBAction)restoreImage:(id)sender {
[fortuneCookieButton setImage:[UIImage imageNamed:#"Image1.jpg"] forState:UIControlStateNormal];
label.hidden = YES;
}
Does the case (upper/lower) of all your file names match exactly for all letters? Source code & project & Mac?
just to share with you, I had this same problem and I found out the solution: I was using lower case in the file name and upper case in code. The thing is on Simulator there was no problem because Mac file system is case-insensitive but in the iPad it didn't work because iOS file system is case sensitive. ;-)
I had this problem. Bizarrely the image had been working in the past, then just stopped appearing on the device one day.
It was a PNG.
I opened it in GIMP and saved it again. Then it went back to working again. No idea why.
Crazy.
Is it the case with only PNG images or also for JPG & other format you're having same issue. If it's only for PNG image, then it might be possible they're not being compressed. Try the following way.
Select your project target & go into its info. Here go into the build tab. In the configuration select "All Configurations" from Configuration section. Now, write "COMPRESS_PNG_FILES' in the search bar. And see the status of the check-box. IF it's unchecked, then make it checked & it will work. But if it's already checked, then there should be other reason in your project.
In the later case, I would request you to create a new dummy project & check by adding 2-3 images whether it's coming or not.
Clear caches delete the previous build and try again.
I also had same problem, and it was caused by a different reason: my project has image files with the same name! Since my app has many images, I inadvertently included images with the same name. Simulator is more tolerant and show the images just fine; however, when built on device, neither of the images of the same name were copied to the build directory. Obviously xcode is more picky for device: since all images go to the same directory, the file names must be different. When there are files with the same name, xcode refuses to copy any of them since it doesn't know which one to pick.
I have the same problem. In my case, the issues is to do with the UILabel above the UIImageView that shows the image. When I change the type of UILabel to AttributedText (In Property Inspector), my real devices cannot load the image. When I change it back to Plain, it works now. I use XCode 6.2.
on top of all suggestions - it wasn't work for me until I restarted the device

iPhone simulator shows app's images but iPhone does not

I have create an application on iphone using objective-c.In this application i am just displaying different players images stored in one folder, which will be run perfectly on simulator. But when I deploy it on iphone it is not showing the images of the player.
for that the code is:
UIImageView *imageplayer = [[UIImageView alloc]initWithFrame: CGRectMake(imgx, imgy+45,135,150)];
imageplayer.image = [UIImage imageNamed:playerpng];
if(imageplayer.image == nil)
[imageplayer setImage:[UIImage imageNamed:playerjpg]];
if(imageplayer.image == nil)
[imageplayer setImage:[UIImage imageNamed:#"noimage.png"]];
[self.view addSubView:imageplayer];
Plz solve this query.
Thanks in advance
Sometimes xCode doesent update correctly the bundle, try cleaning and building again.
I am currently experiencing this problem as well. I was able to correct it after a bit of debugging and reviewing the build file.
When I do an ls -l, I noticed that some of my png files have a # symbol beside the permissions. When I followed that up with a ls -l#, it showed that the files with an '#' had an attribute set 'com.apple.quarantine'. This attribute is given to a file (often from a zip, jar, or other executable) that is downloaded from the net. You can get rid of this attribute by performing:
xattr -d com.apple.quarantine *.png
Then, select the images in XCode, and check that the checkbox under the target is checked. In my case, it was not, so the images weren't being included in the bundle.
I had the same problem, solved by using all lowercase for the file name in code - image.jpg, but it does not seem to make any difference what case the actual file name is, e.g. Image.jpg
check name of image file. It must be "noimage.png"
not Noimage.png or noimage.PNG
Just to clarify, does the "noimage.png" display, or is the program loading a player image but failing to display it? Or is nothing loading.
These should easily be determined in debug mode.
A few additional things to check...
1) Just for testing, start out with PNG versions of the player files. This is the primary format on the iPhone and might eliminate a file format issue or other anomaly that the simulator is not sensitive to.
2) With regard to fixing in an image editor, specifically make sure that the image is set to a DPI of 72 pixels/inch. The iPhone and particularly Interface Builder are very sensitive to this being correct and will sometimes not display or will display a very blurred version of the image if incorrect.
3) Make sure the image(s) haven't been added multiple times (from different directories and/or to different group folders). We encountered a situation where we had inadvertently imported the same images at two different layers within the project hierarchy and this can cause unexpected behavior within the iPhone (selecting randomly or failing to select).
4) Make sure the Get Info -> Targets has your particular target checked. The simulator may still see the image but it will not get deployed to the iPhone.
5) Make sure you can view the image within XCode and that it looks correct.
Barney
This happens to me sometimes. I once spent half a night trying to figure out what was wrong. In this end, this is what worked: open the image with an image editor and save it again. That's all. I'm using the free Acorn image editor to do this. Haven't tried it with Photoshop. (After all, it seems that Photoshop might be responsible for introducing the error in the first place, although that's far from sure.)
I don't know what causes the problem. I usually get it when I use png files that have been sent to me. Could be a simple file permissions problem, or some more subtle problem with the image format. I would be very interested in hearing the opinion of better-informed people.
In any case, if all else fails just try this: open the image with Acorn, save over the original file. Works for me.
Make sure the image files are added to your XCode project.
After some investigation, Michael's comment seems to have a solved it for me. I had all lower case as the file name but was following variable convention and had the png file with an upper case for the second part of the file name. Here my file was putback.png but was referenced in my code as putBack.png
UIImage *putBackImage = [UIImage imageNamed:#"putBack.png"]; //wrong
UIImage *putBackImage = [UIImage imageNamed:#"putback.png"]; //correct
Select correct target while adding image files to your project. Following are steps to make it further clear
Select Add Files to "YourProjectName"
In opening window there is an "Add to targets" option at bottom that shows all available targets
Check target or multiple targets in which you want to add images
Press "Add" button and you are ready to go