Strange issues with file name and Xcode sim vs. ios device - objective-c

I just spent a day trying to figure out why some simple code was not loading images from the resources folder. Just for kicks I went into the finder and renamed one of the files to exactly the same name and it loaded. Then I did the others - simply renaming them to the same name. I looked and there weren't any strange characters or whitespace before or after the file name. So now they all load fine into an array in the simulator. But now on my device, they aren't loaded into the array, returning "nil" and are throwing exceptions. I know that ios devices are case sensitive where the sim is not but I checked this and the naming is all fine. The only thing I can think of is that my images are named for retina (#2x~iphone) and my device is not. But I have images named the same way, for retina, and those load fine.
I am using imageWithContentsOfFile to load the images and also getting a memory warning after it tries to load the images. I am not sure if the memory warning is related.

I think the problem might be in your file naming. Remember that you should include both retina and non-retina graphics in your app. Seeing as you added "~iphone" I'm assuming this is a universal application so your images should be named as follows:
myImage~iphone.png
myImage#2x~iphone.png
myImage~ipad.png
myImage#2x~ipad.png
Then in your code you only reference the "myImage" part of the file name so iOS can sort the rest out for you.
For example:
UIImage *image = [UIImage imageNamed:#"myImage"];

Related

Loading images in WatchKit

I was loading images as background to a WKInerfaceGroup object using setImageName:. But now, I need to do some treatments to the images before they are actually displayed. So, I am calling +imageName: from UIImage with the same exact name, but the image won't load. Why one method works but the other doesn't?
I am using using Xcode 7 beta 4 and Watch OS 2 simulator.
Moving the images in a xcassettes file solved the issue.

UIWebView crashing

I have a problem with using UIWebViews in an objective-C app we're developing. It crashes any Retina iPad. Both iPad 1 and 2 behave fine.
The web view is loaded from local HTML/CSS/JS and is dynamic content that contains pricing information so needs to be essentially a mirror of a website which is pre-downloaded to the device.
The page contains lots of images so I think this is memory related. I've tried reducing the payload on the page which stops the crashes. Obviously the stupidity of Apple to choose to quadruple resolution whilst only doubling memory is a root cause of why it works fine on non-retina devices, but how can I manage memory within the page to prevent iOS from destroying the entire app?
Does iOS automatically store imagery as 2048x1536x32bpp as a bitmap (theoretically 12MB per image?) irrespective of the file format? I've tried converting to JPG / PNG but with no effect on the crashes. Only reducing the volume of images present on the page stops the crashes. It's my first foray into iOS development, so please be gentle!
iOS won't change the resolution of the original images that the page loads, but of course it will de-serialize them from .png/.jpg to a bitmap in memory for display, so you should start by trying to reduce the resolution of the .png/.jpg images that the page loads.

How do I stop Cocos2D from requiring standard definition resources (SD images)?

I am developing an iPhone game with Cocos2d-iphone.
I want my game to only be available to the iPhone 4 AND iPad. Retina enabled for iPhone 4. I don't want the game to run on older devices.
Cocos2d will always ask me for -hd and non-hd files. If I don't provide the SD files, I get errors. I don't want that: is there a way to disable Cocos2d from trying to retrieve SD files, and only get -hd suffixed files by default?
Oh, and when the game is run by an iPad, the graphics will be the -hd ones as well. So the point is, I only want to have -hd files in my project.
What are the proper steps to edit Cocos2d's source for such?
If you are using cocos2d version >= 2.0, then you can change the value of a global variable found in CCFileUtils.h:
static NSString *__suffixiPad =#"-hd";
(Its default value is #"-ipad".)
If you are using cocos2d version < 2.0, then you can find here a category that I wrote to be able to transparently use -hd images created for the iPhone 4 on the iPad 1/2.
Actually, it does more than that, but if you add it to your project, then "-hd" images will be "automagically" used on the iPad 1/2 instead of their SD versions (which you could also not include in your project).
Let me know if you have any issues integrating this code.
If I would have been at your place.. I would have played with a trick.. Don't down vote the answer if you don't like.. But Its just a thing in my mind.
Use -hd images in the code directly.. iPhone 4 will handle it easily.. Older iPhones will show it much larger and it doesn't matter as you are not supporting them...
So instead of using Background.png , directly use Background-hd.png as it will work fine for iPhone 4.. Instead of wasting time on all these SD stuff.. I will directly use my -hd images for my work...
I can not say about new iPad.. But old iPad can easily use -hd images in code as you want.. For iPad I have directly used in my code earlier..
Hope this helps.. :)
This tutorial might help.
If you only provide -hd assets, you should only get errors regarding missing SD assets when running on iPad. The iPad doesn't have a Retina display, but it certainly is high resolution enough to be treated like one.
Since cocos2d passes all filename requests through the fullPathFromRelativePath function in CCFileUtils it may be enough to modify this function to treat the iPad like an HD device and force it to load -hd assets on iPad.
the 1st idea i can think of is to use #2x suffix for images (UIKit style) and you wont need to enable retina display in app.The problem is that on the ipad you'll have to write your own method to remove #2x suffix
the 2nd idea was to make a SD image..but only 1x1 pixels... it takes virtually no space at all ( 119 bytes with white pixel). Should work on ipad as-is
3rd idea:don't enable (or enable..doesn't make any difference) retina display but have the HD images as SD images.I've tested this on cocos2d v1.0.1 and it seems to work.Also..it should work on ipad
4th idea forcefully enable retina display in ccConfig.h (so that director won't ask you for the SD images on ipad) .I'm not sure about this one because there are quite a few tests that check for resolution, device and if it's retina and you'd have to edit all of them
these are just ideas off the top of my head..they may be wrong..but they're just ideas

Why are images shown only on simulator but not on device (iPad)

My array has the value:
imageArray = [[NSArray alloc]initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png",#"image6.png",#"image7.png",#"image8.png",#"image9.png",#"image10.png",#"image1.png",#"London.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png",#"image6.png",#"image7.png",#"image8.png",#"image9.png",#"image10.png",nil];
I want to show these images in a scrollview. All the images appear except #"London.png", which is appearing as blank. How do I fix this?
I've come across the same problem when i wasn't carefull about capital letters in file names. For example file Image.png (if called as #"image.png" from code) was normally shown in simulator but not on device. So make sure your names match exactly. Is your file London.png actually london.png?
And it has to be added to the project and to the target.
...if error persists try to save again your png files.
sometimes png files are not saved in the right format, and even if you can see them on a mac (and in simulator) they are not readable in IOS devices
1 - Open your [myApp].app product folder and check your images are really embedded.
2 - Check that your image is really a png and not a simply renamed jpeg.
Besides the case, images are even displayed without the extension on the simulator.
While [UIImage imageNamed:#"test"] does work on the simulator, it will not on an actual device, it has to be [UIImage imageNamed:#"test.png"]

Can't find image while running on device, but can find it while running on simulator

I'm trying to load an UIImageView with an UIImage using [UIImage imageNamed:name]. The simulator works perfectly loading the image, but running the same code on an iPad terminates with an exception (because the UIImage ends up being nil). Why is this happening?
I added the images from XCode to 'Supporting Files/Images'.
Weird thing is that some files I added are being found ok, but these other images are not. The only difference between them is that I renamed the ones I cannot find from within XCode. I even tried removing and adding them back again, but no dice.
Case is not the issue, I already checked it.
This is on XCode 4.2 and iOS 5.
Any clues?
Thanks
Image names are case-sensitive on the device, but not on the simulator.
If your case is correct, reset the simulator.
http://www.jameskelso.com/post/9627437152/images-show-in-the-ios-simulator-but-not-on-a-device
Make sure that the images exist in the "Copy Bundle Resources" section of your target's Build Phases. Whatever you do, try cleaning your build folder first (option+shift+cmd+k). Good luck!