Cannot load PNG image as UIImage - ios

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.

Related

AppCode warning "Resource not found" when using Asset Catalog

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.

Bundle a LOT of images/icons

What would be the best way to include a lot of images in the bundle? I have an index of (game) items thus about 4000-5000 image files (total 27mb so not that big). Just include the whole map in the bundle or maybe first write a script that converts them to NSData? I could imagine there would be a smart way to do this so the app wouldn't have to look through all images individually to find a single one. Would love to hear your thoughts.
27mb isn't a huge amount to download so the easiest option would be to put them in an asset catalog, as Ryan Heitner mentioned in his comment this will allow App Thinning to take place in iOS 9.
I'm not sure what you mean by this:
I could imagine there would be a smart way to do this so the app wouldn't have to look through all images individually to find a single one
Each image will need to have a unique name (this is true regardless of the number of assets you have) and your code references the images by that name so it won't have to "look through all images individually to find a single one".
Alternatively if you really want to reduce the initial download size you could use On Demand Resources (another upcoming iOS 9 feature) to store them on Apple servers and loaded on demand in your code. Presumably you won't be targeting only iOS 9 though so in this case you would need to host the resources yourself and load them using standard techniques (see here, here, here, or use a library.
You should pack them in a texture atlas.
Then, the texture atlas files should be imported in your bundled via a folder reference (blue folder icon) and not a group (yellow folder icon).
Images imported in bundle in folder reference won't be optimized be Xcode on packaging. So you can make your own file optimization using imageOptim. It can compress a lot more than what Xcode can do on JPEG and PNG images.

RW IOSFramework tutorial possible bug using bundle?

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

Finding image resource in iOS app (XCode 4.2)

This feels like a silly question but I'm stumped. I working on an iOS app using XCode 4.2 and I can't find an image file anywhere in the project. The image filename is passed to UIImage imageNamed: so it has to be somewhere local (not downloading it from remote server). What's the best way to find the image?
In my own iOS projects it's trivially easy to locate images: look in the Resources group in the project directory. I've inherited this application and looking in Resources turns up nothing.
The bottom of the navigation panel has an input field which filters what's displayed
Try the Supporting Files folder

iPad: PNG Crush error

I am trying port my iPad project to latest iPad 2. I installed iOS 4.3 SDK with Xcode 4.0. When i try to build my project, i'm getting the following error. I don't know why am i getting this error. Could someone help me to resolve this build error?
While reading /Users/username/Desktop/iPAD/MyProject/trunk/Resourses/images/top_bar.png pngcrush caught libpng error:
Not a PNG file..
Could not find file: /Users/username/Library/Developer/Xcode/DerivedData/Project-fmhveawksgdtnraclfizuhrekmzi/Build/Products/Debug-iphoneos/MyApp-Upgraded.app/top_bar.png
I had the same problem, but it wasn't because of a corrupted png : it was because two PNG files had the same filename in different directories of my source tree. What was peculiar was that the errors (I had 4 files which had "twins") were not identical from one build to another. On one run I had errors on all files, on the other only on some of them.
Did you check whether the file Users/kavapanga/Desktop/iPAD/ALN II 3 latest 2/trunk/Resourses/images/CS_logo_for_top_bar.png exists? Also check if it is infact a PNG file. Right click on the file and do a Get Info to check if the file has any other extension and is marked as hide extension. If you are not sure of the format of the file, opening the file and save it as a PNG file again should work ,most of the time.
I had the same problem.
How to fix : Open up image with Preview -> File > Export > Format change to PNG and you are done !!
There is no issues with image or Xcode. The issue, which creating that image from photoshop, is whether that image is interlaced or not interlaced. While saving your image in photoshop you will get one option for that.
Check out this link for more info:
Build app with Xcode 4 - it always show some error about PNG image
not interlaced: You should use image which is not interlaced. This is the original png image.
interlaced: This option is suitable for the image which are getting downloaded from web. Specifically saying, this is used when you require to reduce image load time. What this option internally does is, it will create a .png file but with lower clarity and lower dpi like a normal jpeg image. That's why such images are having property that it is jpeg image.
Hope this will help you all.
Enjoy Coding :)
I got this error when I moved images around in the project's folder, to an "Images" folder.
Turns out my "Copy Resources" build phase contained the steps both to copy the files from their new, correct folder - and from the previous folder where they were stored. Removing the outdated build steps fixed the errors.
Another reason pngcrush considers PNG files to not really be PNG files is if you accidentally export them with layers intact. If you inspect the file in Finder, look around the More Info area (Dimensions, Color Space, etc.) for an item called "Layers"
It might say something like:
Layer 68, Title Banner, Group 26
This happens when designers sometimes Save As PNG instead of exporting for web/mobile. And it completely destroys libpng (and thus pngcrush).
The fix is to properly export the PNG, or open the PNG yourself and re-export it as a "flat" PNG.
This can be caused by incompatible PNG file format. You will need the "Color profile" for your png file to make it work in Xcode.
First off, check the info for your file.
While this is a valid PNG file, this is NOT the format that Xcode can take. You will need the following PNG format:
To convert your PNG file to a format that Xcode can take. You can use Preview to export the file.
Had this issue as well. The PNG existed and wasn't corrupt.
Solved it by:
R-click the image in xcode, select "Delete"
Remove reference only (don't actually trash the file)
Drag n' drop it back into xcode (in-place) making sure you select the correct targets
I had this problem and found there was a problem with the png. I remade it and worked fine.
I had the same problem. Even though the file extension may be png the file may still be something else. And 'get info' shows what ever the file extension is I think. Open up the file in preview and go to tools -> show inspector -> File tab. Here you should be able to see the actual file type.
I had this error and it was actually totally unrelated to the png's it was erroring on. The actual problem was I had two files with the same name in my project (I had drag'n'dropped a newer version of a 3rd party class assuming it would replace the older version).
I removed the reference to the duplicate file and that solved the build errors.
Problem Solved
I got the same problem couple of time in development but today i found that there are two solutions and their possibilities, here is this
Problem Solved#1: If you Create or your designer create the design which they saved the pnd from adobe illustrator then its got the problem if you create by yourself just copy all the AI artwork to the photoshop or if your designer create it then just tell them to import the illustrator format to the photoshop and send the pnd.
Problem Solved#2: select the image right click the image->get Info and check that it is in correct .png format or not if its not then just edit .png extension.
Just Enjoy.
I get the same error when I tried to copy my project folder and run the project. The project in the original folder didn't have that problem.
Deleting the copy and making a new copy of the folder solved my problem.
Error:
While reading /Volumes/Mac OS/RDC/Workpot/RestApp/RestApp/default1024_768.png pngcrush caught
libpng error: Not a PNG file..
Solution:
I opened "default1024_768" file in PaintBrush tool and saveAs PNG file with same name.
Replaced the Icon file in Xcode project --> Clean --> Build.
That's All its work like charm
Hope this will help someone.
I have this bug quite often. if you 100% sure that your png is there and is correct then just simply recompile the project, if does not work - recompile again and again, and sometimes it will start working ;-) that's a bug in Xcode that was there for long time (since Xcode3).

Resources