According to the UIImage Class Reference: Discussion This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object. On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of 1.0.
If the screen has a scale of 2.0, this method first searches for an image file with the same filename with an @2x suffix appended to it. For example, if the file’s name is button, it first searches for button@2x. If it finds a 2x, it loads that image and sets the scale property of the returned UIImage object to 2.0.
Otherwise, it loads the unmodified filename and sets the scale property to 1.0. See iOS Application Programming Guide for more information on supporting images with different scale factors. Special Considerations On iOS 4 and later, the name of the file is not required to specify the filename extension.
Prior to iOS 4, you must specify the filename extension. Since you're targeting iOS 4.0 and later, you should not need the filename extension. I tried to reproduce this potential bug, but it worked as expected for me without the filename extensions.
Here are some ideas for what may have gone wrong to create this effect in your app: It's possible that the problem comes from the cache if you changed your images at some point. If you choose not to use the filename extensions, and you have both "background. Jpg" and "background.
Png" as options, the preference appears to be to use the ". Png" file. If you set the target to iOS 4.0 and later after first running the app, then the filename extension would have been required and the image may have been cached blank, which leads back to theory 1.
That's the best I can make of this.
Then it has to be some problem with my image – Krishnabhadra Jul 15 at 3:14 If you set the target to iOS 4.0 and later after first running the app, then the filename extension would have been required and the image may have been cached blank.. from the start of the project, iOS 4.0 and later is targeted..Also all my other images (which are png's) are referred without extension from the start, and they worked fine (saying that I never build this app targeting OS before 4.0).. – Krishnabhadra Jul 15 at 3:16 @krishnabhadra... yes, I started with jpgs without extensions and they loaded fine. I had no pngs and nothing complicated (dummy app just to test this). Very strange.
– PengOne Jul 15 at 5:25.
Write a wrapper to get the image like -(UIImage*) getImage:(NSString*)imageName{ UIImage *image; if (UIScreen mainScreen respondsToSelector:@selector(scale) == YES && UIScreen mainScreen scale == 2.00) { // RETINA DISPLAY NSString *jpegFile = imageName stringByAppendingString:@"@2x. Jpg"; image = UIImage imageNamed:jpegFile; } else{ NSString *jpegFile = imageName stringByAppendingString:@". Jpg"; image = UIImage imageNamed:jpegFile; } return image; } And from your code call bgImageView.
Image = getImage(@"background").
Good thinking @Chandan..I have already written a similar wrapper and it worked beautifully..But I still changed my images to png to use Apple's native imageNamed and reduce the work load (since I have a lot of images in my project)..But your code is very useful.. – Krishnabhadra Jul 13 at 9:29 changing . Jpg files to . Png will increase the image size hence the bundle size... If it is not a problem then fine.
– Chandan Shetty SP Jul 13 at 9:36 changing . Jpg files to . Png will increase the image size hence the bundle size ... Are you sure about that?
I thought png's are more light weight.. – Krishnabhadra Jul 13 at 10:04 Also see my new edit.. – Krishnabhadra Jul 13 at 10:05 . Png images contain alpha... So the size will be more. Just check the size of single .
Jpg and verify it with by converting the same file to . Png – Chandan Shetty SP Jul 13 at 11:31.
You just need to provide the full name of image. BgImageView. Image = UIImage imageNamed:@"background.
Jpg"; don't leave off the extension. As for the @2x, it is not necessary to call it out anywhere in the code. If you code correctly there is no need to test for a retina display as some here have suggested.
I already tried as you have suggested. I mean I have used the exact same line of code as you have in the answer. And I am getting the image on my 3GS phone.
But my iphone 4 is also displaying my 1x image, and it seems stretched and unclear.. – Krishnabhadra Jul 9 at 3:18 I guess it's not a programmatically issue. Have you checked the correct linking of your background@2x. Png file to your XCode project?
Do you have the same issue with the iOS Retina Simluator? – Florian Mielke Jul 12 at 8:15 sorry @Florian..didn't see your comment..Ya I am having the same issue in iphone simulator too.. – Krishnabhadra Jul 13 at 10:02.
BgImageView. Image = UIImage imageNamed:@"background@2x. Jpg"; If you don't provide the file extension for an image, iOS assumes that the extension is png.
But, because your image is jpg, it can't find it. Always provide the extension, especially for backwards compatibility, as iOS 3.0 doesn't assume anything and just searches for the exact given file name. Also, there isn't much point having 2 images when you can just have 1 hi res image.
Lower resolution devices will automatically scale it down (UIViewContentMode must be set to AspectFit, AspectFill, or ScaleToFill), and it will display fine. I always use single images and never have a problem.
Thanks for the response... If you don't provide the file extension for an image, iOS assumes that the extension is png . Are you sure about this? Is it documented anywhere (in apple docs I mean)?
– Krishnabhadra Jul 9 at 3:18 always provide the extension, especially for backwards compatibility I am targeting iOS 4.0 and above, which I mentioned in question.. – Krishnabhadra Jul 9 at 3:21 Lower resolution devices will automatically scale it down (UIViewContentMode must be set to AspectFit, AspectFill, or ScaleToFill), and it will display fine. I always use single images and never have a problem Ya I know about it..But I think it comes with a certain performance penalty and it is better to use different images for both 1x and 2x..Correct me if I am wrong but that is the whole reason why imageNamed changed to support both 1x and 2x images.. – Krishnabhadra Jul 9 at 3:29.
You can use jpgs, just tried it myself and it worked. Only suggestion is how you are instantiating it. Try it this way: stockImage = UIImageView allocinitWithImage:UIImage imageNamed:@"Morbo.
Jpg"; stockImage. Frame = CGRectMake(0,0, self.view.bounds.size. Width, self.view.bounds.size.
Height) ; self. View addSubview:stockImage; This is the image I used:
.1 Yes @elmo, When I give image name with extension jpg images are displaying fine. But when I give UIImage imageNamed:@"Morbo" then jpg images are not showing...But when I give extension 2x images are not loaded in iphone 4...I think you have not read my question correctly.. – Krishnabhadra Jul 13 at 2:29 1 Sorry you're right, dyslexia is a bit of a curse for a programmer ;) thought the general implication was that jpgs cant be used. I just remember first trying to use images and when they didn't work I just added the extension like in web programming – Elmo Jul 13 at 8:03 I am sure jpg's can be used..I got it working..The only question is whether UIImage imageNamed: handles jpg image the same way as png's...For png's they understand 1x and 2x images without giving extension..But for jpg's they simply takes nothing without extension.. – Krishnabhadra Jul 13 at 8:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.