Image loading into UIView - ios

I'm loading an image into my UIView background as a background color from a file stored in my xcode project. It works fine on the iPhone simulator but when I compile on my iPhone, the image isn't there in the background, which is obviously due to the fact that the image isn't stored as a file on phone. Is there a way to hard code an image so it's transported to different devices, forgoing the need to have an image file present?

When dragging a image(or any other reseources), be sure to check this option: Copy items into destination group's folder. In this way, your image will be store in the app bundle, instead to just reference from some path inside your pc.
Here's of this checkbox:

Have you tried
adding the image to the bundle in the copy files build phase?
This will put the image into your app so it will always be there.

Image load important point:
1.Make sure your image below your project document, the true path or relative path.
2.Image name differentiate uppercase and lowercase on the iphone but not use the iphone simulator.
3.Use Xcode shotcuts 'command+shift+K' to clear your project then run again, judge for if the image cached or not.
Run run run!

Happened to me once as well. Remember that if you're using [UIImage imageNamed:#"imageName"];, then with the iOS simulator, you don't need the letters to be in the right case. If your image is named "IMAge.png", then the simulator would find it with the parameter "image.png", but the device wouldn't. So make sure you've got the correct name, the device is case-sensitive.

Related

Image suggestions are not working Xcode 10

I am trying to set image to UIImageView. Its working well in older version.
In Xcode 9.4.1
But in Xcode 10
When I am trying to type name of image, image suggestion is not showing.
Even "Image Literal" is not working. Double clicking on above icon also not working.
Is there any setting in Xcode preferences?
How to enable image suggestion like before?
In Xcode 10 and Swift 4.2, only the code completion function (or auto-complete) of the Xcode IDE has been discontinued for the old way. Here is the new way:
Just type image literal and it will complete with default icon.
Double click on this icon and it will open the media library right side of it.
Just choose your image and it work like before!
Hope I am able to help you!
https://download.developer.apple.com/Developer_Tools/Xcode_10_beta_3/Release_Notes_for_Xcode_10_beta_3.pdf
Code Completion for Swift image literals has been removed in Xcode 10. (38087260)
There seem to be some real issues with how Xcode 10 handles image literals, especially when the name you provide your assets doesn't align with the filename of those assets.
The following are the steps I've found will restore image literal previews.
Add an image via the media library picker.
If you have multiple resolutions for your image (e.g. #1x, #2x, #3x), Xcode will inconveniently drop them all into your code and throw an error to let you know you've done something naughty.
Delete two of the faulty image previews, if necessary. You may be left with a default image icon instead of a preview of your actual image.
Comment out the lines on which you're setting the image. You'll see the image literal references the filename of your asset rather than the name you've provided in the asset catalog.
If you've renamed your assets after adding them to your asset catalog, this seems to sometimes prevent Xcode from reading the asset appropriately.
Change the filenames to the appropriate asset names.
You can find the asset names in your asset catalog.
Uncomment the code and, if the default image icon is still visible, double click on the image icon. It should then display a preview of your actual asset.
You may also need to clean and rebuild your project.
use command + shift + M to popup the Media Library then Simply Drag and Drop into your Code
OR

Image does not update in Launch Storyboard

I have noticed that if I change an image in the xcassets, the launch storyboard does not update.
For instance, let's say you have a UIImage view in your launch storyboard with an image called "logo" that is blue. If you open up the in the Finder and change the color of the image to red, next time you run on the device the logo will still be blue.
The preview of the storyboard in xCode will show the correct (red) logo image.
Before running, I have tried:
Cleaning the Project
Deleting Derived Data
Deleting the App
Renaming the Image
Nothing seems to fix the problem. Is there another option that I am missing?
Appears to be a problem with caching on the device. Delete your app, restart the device, and pray. Image does not update when changed if that image is used on LaunchScreen.storyboard
Exiting out should normally solve the job... Sometimes, especially in the playground I have heard and experienced, it will not automatically refresh and you can either:
Wait a tiny bit(in most cases) but it seems you would not be asking if it were a few seconds of delay so maybe try
Deleting everything associated with that image(the outlet reference, image on storyboard, and any time you use that image in you code(leave those spaces blank)), then create a new UIImageView and assign it to your desired image.
In your Images.xcassetts file you may have only changed the info on one of the sizes; make sure those are all set.
Then again, especially after the most recent WWDC, there have been some bugs in Xcode that will need to be patched in the future. At the least, I hope this helps you and people who view this in the future.
I tried deleting the app from device while also deleting the derived data and cleaning, but it seems that you have to restart the device. That is the only thing that worked for me.

How To Load The Image Set(The Bundle) From Images.xcasset in iOS

When I am developing apps in iOS before, I always put the image sources of the app at the root directory of the project, and then I will load them using [UIImage imageNamed:#"image.png"]. I did't even include various sizes and resolutions such as image#2x or image#3x. But I am afraid this is not a good practice, because I cannot deal with different screen resolutions.
Now, I want to make use of the Images.xcasset to store all my image sources so that I will be able to load just the bundle name, and hoping that the system will pick the image with the right resolution automatically. Therefore, I made a test and place my image set under Images.xcasset and named it as images, and then in my UIViewController.m I am trying to load the image by calling [UIImage imageName:images]. As a result, it didn't work. So I searched for an answer and found out that I should call [UIImage imageName:#"images60x60#2x.png"] in order to load the the 60pt #2x image.
But I think this still did not solve my problem, because I am still choosing which image to load. Is there a way to load the entire image set or load the image according to the resolution of the screen?
Please help. Many Thanks.
EDIT: Added Screen Shots
I use AppIcon as a test
As you said at last that you are using AppIcon as a test. Please don't AppIcon it is made for internal use. try to create you own imageSet and use that. It should work.
Two things :
As said Ankit, do not use App Icon, use your own set
do not use a file suffix. Just reference [UIImage imageName:#"MyImage"] (not [UIImage imageName:#"MyImage.png"])

UIImage imageNamed returns nil

I am pretty new to iOS and Xcode.
I tried to load an image with
[UIImage imageNamed:#"imageName.png/jpg and so on"];
but it only returns nil. The image should be in my project bundle, considering the fact that I can choose it from drag and drop menus in the InterfaceBuilder and it was added to Xcode via the "Add files to "projectname"..." menu.
It makes no difference whether I use .png or .jpg images, the result stays the same: they work via IB but not if I try to run the imageNamed method myself.
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.
Re Mike Weller's comment. The checkbox is ....
If you verified all of the above and are finding that your UIImage(named: "myImage") returns nil from code inside of a dynamic framework you are building, then you have to change the code to:
UIImage(named: "myImage", in: Bundle(identifier: "com.myframework.name"), compatibleWith: nil)
This should retrieve the asset from CXAssets correctly.
In my case, the PNG was malformed.
For some reason Xcode preview shown it correctly, but when I tried loading it with UIImage, it returned nil.
I just had this issue for jpg files named on disk "file.jpg"
I was trying to load without the extension, just #"file". While this works fine for pngs, it does not for jpg. Once I used #"file.jpg", it worked!
Swift 5
If you get UIImage nil in dynamic framework, you should set image with bundle identifier.
You can create an extension to Bundle like this;
Create an extension;
extension Bundle {
public static let myFramework = Bundle(identifier: "com.myFramework.bundle")
}
Then you can use like this;
UIImage(named: "myImageName", in: Bundle.myFramework, compatibleWith: nil)
Or
You can use like this;
UIImage(named: "myImageName", in: Bundle(for: type(of: self)), compatibleWith: nil)
I think the first one is more useful because it's more readable than second one.
Try re-exporting your image.
I had a similar problem, UIImage imageNamed was returning nil and none of these answers fixed my problem.
I found out that it was actually something wrong with the png file. I opened the file in GIMP image editor, saved it as a new file, exported it again as png and magically it started working.
I didn't change anything in code at all so there was definitely something wrong with the actual image file.
try
[UIImage imageNamed:#"imageName.png"];
instead (with an extension)
I had the same problem: #"photo.jpg" resulted in 'nil' UIImage.
Changing to the "actual" filespec, #"photo.JPG" works fine!
I hadn't realized there was case-sensitivity there! VERY non-intuitive.
i had a similar issue, finally the cleanup fixed it: the image showed up in the simulator, but id did not show up when running the app on the iPhone.
What I did:
1) deleted the app from the iPhone
2) performed Product/Clean
after that the bug was fixed and the image showed up!
Try to use UIImage(contentsOfFile:) instead of imageNamed. It worked for me with downloaded images.
Check that file has no space at the end.
The name like name #2x.png will fail loading, but name#2x.png is fine
Does the image work on the simulator but not on the device?
If so, in this scenario it is always to do with the case of the name. The device requires exact case and the simulator is not so fussy if I remember correctly.
e.g. for an image named image.png
[UIImage imageNamed:#"Image"];
would work on the simulator but not on the device...
Make sure you have the image for the DEVICE you are using in your .xcassets file. An image flagged for iPad won't show up on the phone (or the simulated phone).
In addition to #Inturbidus's answer:
For those who came here working on Application Extensions:
The image you are working with should belongs to both targets - hosted app and extension.
In other case you'll always getting nil trying to access it from within the extension's code.
For Xcode 6.4, click on Images.xcassets and check whether there are entries for each image that you called. You may right-click on the image and select "Show in Finder" to check if the images are the correct ones added.
Make sure the Attributes Inspector name field of the image matches exactly the name you're using. In my case the image was part of a folder and so the image name was "folder/name". Using this long name solved the problem. XCode 7.3.1
In my case, using a name with accented characters proved to be a poor idea. I changed Astéroïdes to Asteroids and it worked fine.
In my case problem was with image name in asset catalog.
For example if you have image with name "TEst" and change name to "Test", changes won't be indexed. If your teammate will pull commit with this changes, image in his asset catalog will have previous name "TEst".
Very small chance to catch this situation but it's possible.
I had multiple .xcassets in my projects and 2 had duplicate name of images. So it was not loading the image in my case.

Could not load the image referenced from a nib in the bundle with identifier

I had got an iPad application.The ViewContrtoller.xib has got an UIImageView in it. The UIImage of the UIImageView is set through Interface Builder. When I run the application in simulator everything is fine. But when I am trying to run it in iPad the UIImage is not loaded. The message that I am getting is:
HexaPuzzle1.0[3259:207] Could not load the "start.png" image referenced from a nib in the bundle with identifier "com.yourcompany.HexaPuzzle1-0"
2010-12-17 10:57:27.221 HexaPuzzle1.0[3259:207] HexaPuzzle loaded
Any help would be appreciated and will be a great help for me.
The issue has been rectified.
Problem was with the image format. When the image has been edited and saved as PNG-24 everything goes fine.
The application now runs smoothly in simulator and device.
Thanks for all friends who suggested a solution and tried to helped me.
Regards,
Rupesh R Menon
I know it's very late, but this is a solution that also works in Xcode 5, and therefore likely to be useful to people who have this problem in the future.
I have seen a lot of questions like this on the web but none of them seems to have one clear answer. This is, as far as I know, the solution for this problem:
Go to the image in Xcode (no need to re-add or re-generate the
image)
click "show the file inspector"
Make sure the checkbox in the category "Target Membership" is checked. the test check box does not have to be checked to make your app load the image.
The target membership tab looks like this:
if this is already the case, you are working with a broken or damaged file. to rule this out, check if another app (like photoshop) can open your image.
This should do it, hope it helps!
When dragging your start.png file into your project, did you check this option?
I was getting this error in the simulator after upgrading to Xcode 7 / iOS 9 and opening an existing project. The image was present and named correctly, within an XCAssets package.
Turns out the problem was with a parameter within the XCAssets package.
Changing Render As from Default to Original Image solved the problem:
I've had the same problem before. The png file format is not right. Try re-generate the png image file and the problem will be solved.
None of the above worked for me, and the message I got was "could not load the "(null)" image referenced... which means I could not look for the image. Very frustrating. In the end it became a trial and error exercise of going through every xib and storyboard looking for potential conflicts. I finally found it - I had specified a "Selected Image" for a tab bar item which was not needed. As soon as I got rid of that the problem went away.
None of these things work for me. I created a new folder in the projects where I placed the pictures. That worked for me.
This problem was fixed when I changed "add folder reference..." to "add group..." when I import the images to my project. I'd prefer to have folders with live updating, but at least it works now.
Had an image on a XIB (which is replaced by the correct one in code). After finishing this XIB, I renamed the images. So XIB was now referencing a non-existent image.
There is also another potential cause of this problem: If you upgraded to XCode 5 and use both the image asset catalog and "normal" images copied to your project (like you did prior XCode 5). If you then have images with identical names (e.g. "dog.png" and a image set called "dog") it might work in your storyboard and in the simulator, but you'll get this message when run on a real device (where the image will not be shown).
Try moving the images directly to the Resources directory (not in a sub folder)
Make sure the resource isn't too big to be handle.
If you get the error message with the name of the picture, you can search it inside your project and xCode will display immediately the UIImageView with the wrong picture inside UIStoryboard:
Make sure it is included in your Target Membership
Select your "Images.xcassets" file in the file Inspector
Open File Inspector (cmd + opt + 1)
Select your target membership (Usually 1st line)
I had the same problem, but the cause was very different. I was using image sets in the Asset Catalog that varied based on the device idiom: iPad or iPhone.
The framework target they were being added to had been accidentally marked as as having a deployment device type of iPhone, so the images weren't showing up on iPads - they weren't even being copied into the .car file in the framework.
Setting the deployment device type to Universal, as it should have been, solved the problem.
For some reason I was getting error until I put the image in the Assets.xcassets

Resources