I have added an image set to the asset catalog (in Xcode) and then intended to access this in code.
The code I am using (in AppCode) is...
UIImage *image = [UIImage imageNamed:#"Photo_Placeholder"];
I know this works and is correct. However, AppCode is telling me "Resource Not Found".
I know that this is only a warning and the build works anyway but I wondered if there is a way to get AppCode to recognise the names of the image sets in an Asset Catalog? At the moment it auto completes the image name based on the files not the image set name.
Sorry for the inconvenience, it's a known problem in current AppCode version. Please, follow this issue in our tracker to receive updates.
Related
In Xcode 8, you have images autocompleted while typing.
Question is : Why?
I tried to init UIImage with the suggested result, but it doesn't work.
Does anyone know how to use it ?
Xcode 8 will automatically recognize any images you’ve got in an Asset Catalog and offer them up as a suggestion inside of a UIImage initializer.
So basically what you need to do is just the following (as you have done in your question, but there must be something else that´s disturbing it):
let i = voiture // image name
And then just use i when you want to set an image.
Under the hood it’s creating code that looks like this: #imageLiteral(resourceName: "voiture.png"). But inline in the source editor, you’ll just see the file name of the image. The #imageLiteral syntax is only recognised on Swift 3 or later.
Here is a demonstration video link where I do this and here is a link to a sample project that I created.
Note that you need to click on the instellisense suggestion so that you see a thumbnail of the image in the code and then the image name.
Update 1
This functionality remains in Xcode Version 9.0 beta 6 (9M214v)
Update 2
Xcode 11.2.1, this is not appearing anymore.
An image literal already is a UIImage, you don't need to pass it to a UIImage to initialize one.
I'm unable to find an "official" source for it, but from my personal testing it appears that while the autocomplete works in both Swift 2.3 and Swift 3.0 projects, image literals will only work correctly in a Swift 3 project.
Give it a try in a Swift 3 playground and you'll find it works just fine.
I have been using this feature. Basically if I have an image that begins with "ic", I would type let i = followed by ic. At this point, Xcode 8 will show a dropdown of images and I then choose the correct image from the list. I have never bothered to actually type UIImage(. It works just like autocompleting code except that it uses the filename of the image.
In your case, there is probably some other issue causing the compilation to fail.
I am a newbie on iOS development and am developing my first serious app (Objective C).
I am using an external API and have gotten to a point where I need to download an image from said API to keep offline as cache.
Now, if I understood correctly I can add images to my app on XCode using the asset catalog. For example, if I add an image with image#1X.png, image#2X.png and image#3X.png versions, in order to use them later I only need to provide the "image" part to iOS and it will automatically return the version appropriate for the currently used device.
I cannot however add images to the asset catalog on runtime - I must create an NSData from the image URL and save it to the Documents folder.
The thing is, this API gives me a bunch of URL's for different versions of the image I need to download and among them are the #2X, #3X etc versions of the image, so I need to download all versions and use the asset catalog feature of retrieving the appropriate one for the device.
How can I achieve this?
Thank you very much in advance for your help and please correct me if I got something wrong.
so I need to download all versions and use the asset catalog feature of retrieving the appropriate one for the device.
You don't need to. We add 2x and 3x images to our asset catalog because we don't know on what device our app is running. But in your case, you need to know this at runtime, so you already know what's the device.
To get the scale factor for your device you can do:
UIScreen.mainScreen().scale
So, for 2x this will return 2 and for 3x it will return 3.
From this, you can know what's the proper image you need to download from your API instead of downloading them all which would add unneeded overhead.
You can check the document on the UIImageAsset if it helps in your requirement
Also as you say you are newer to iOS development you can check file system document also which helps you in understanding and managing file in better way
File System basics
Edit: as mentioned by sateesh and patchdiaz, it will be better solution to download only single resolution images for the particular device instead of downloading all resolution images for a device
My image assets fail to load for a nib in my iOS app, but I have added them with interface builder's dropdown that lists my files.
I get an error message saying that it failed to load "Foo.png", but I can see this in my bundle, and Interface Builder populates its drop down selection box for the image with "Foo.png" as well.
Foo.png is in a subfolder in the bundle called "Textures"
Xcode seems to go out of its way to hide paths and stop you from needing to specify them fully. However, this can cause confusion for both the developer and the tools themselves.
Specifically, in Interface Builder you need to fully qualify the path relative to the bundle root. If you do not do this then the asset will fail to load along with any #2x, #3x versions of it you may have placed next to it. This means that Interface Builder will show you a broken image when you specify the path correctly, however the image will show on device and in the simulator when running the app.
This bug has been present in all versions of Xcode I have used from 3.x through to 6.2. Hopefully it is fixed in a future version. I believe I have reported these long ago, I would highly recommend every developer who uses these features to report these bugs to Apple too.
Note that due to another long standing set of Xcode bugs it is necessary to delete the bundle from the device and do a clean build to ensure that this change has any effect when running the app to test if it fixes the issue.
I am trying to load a .PNG image located in my XCode project as UIImage - but the constructor of UIImage that takes file name returns nil.
Please see the screenshot for my setup:
Here are some details:
The project is in Swift
I am constructing the UIImage in global variable (lazyly)
The issue is pressent in simulator and on the device (iPhone 5S)
I have added same image several times with different names to make sure incorrect naming is not the cause (see all the names in left red rectange)
all images are added to EasyTypeShared target, which is a library project embedded into runeable EasyType project.
I did not do any axtra steps - just added the image files to my workspace (and the EasyTypeShared target) and run the app
EDIT: According to this blog the cause might be that workspace setup - more preciselly that the image is being loaded by framework. Not sure but in lack of better ideas I will follow this track. If you can cast some light on the matter I would by really thankful for any ideas.
The problem was caused by the fact that code loading the image from resources resides in framework. Main bundle does not contain resources for the framework, the corresponding bundle needs to be used. Working code:
private let imagePlaceholder = UIImage(named: "ImagePlaceholder.png", inBundle: NSBundle(forClass: CarretMoveController.self)
Are these part of the images.xcassets? If not they will have to be read using NSBundle and getting a path/URL for the resource and all that and a simple filename will not suffice.
You can add the files to the assets as per the Apple documentation here. Relevant section might be the 'Adding Image Sets' one.
If you do not have images.xcassets, you can add it to your project as per one of the answers given here.
in your project explorer I can see that images are not named properly.
it should be like this:
imagePlaceHolder.png
imagePlaceHolder#2x.png
notice the png at the end of the file name, and #2x before the file extension.
Trying to build my own framework, i followed this tutorial
https://github.com/sammyd/RW-iOSFramework/blob/master/BuildAFramework.md
on how to build a framework.
and i noticed there is an issue with loading images from bundle
I downloaded the final result at
https://github.com/sammyd/RW-iOSFramework/
and after running the app i noticed that the 'ribbon' image isn't shown.
placing a breakpoint at RWRibbonView.m at
UIImage *image = [UIImage imageNamed:#"RWUIControls.bundle/RWRibbon"];
result in image = nil.
anyone know how to extract the image from the bundle ? or direct me to a different tutorial ?
p.s before testing, i changed the scheme to the aggregate and build it just to make sure.
Thanks!
This problem seems to manifest itself in Xcode 6 - where, rather than copying the resources bundle into the dev app (as expected) it instead attempts to link against it. You can see the problem in the following image:
You can fix this by dragging RWUIControls.bundle from the "Link Binary With Libraries" section to the "Copy Bundle Resources" section:
Note that this change has been made in the repo on Github, so you should not have any problems if you pull the latest changes.
https://github.com/sammyd/RW-iOSFramework
Hope that helps
sam