Xcode 6 iOS 8 UIImage imageNamed from bundle issue - ios

I build my project using iOS 7.1 and try to load UIImage view with image that is stored in the /images/cars/car_1.png
All images are located in the folder images as on picture below in project tree:
So it works perfect for iOS 7.1 and Xcode 5, but when I try to use Xcode 6 and iOS 8 the UIImage instance is equal nil when I try crate image.
UIImage *image = [UIImage imageNamed:#"/images/cars/car_1.png"];
po image
nil (for iOS 8)
it can be also
UIImage *image = [UIImage imageNamed:#"/images/sport-cars/car_1.png"];
as you can see the name of resources is the same car_1.png but it is ok because they are in different resources folders not in the bundle folders.

Ok the problem is that on the simulator and seems on the device as well by some reason when we use #"/Reference Folder Path/image.jpg" it won't work.
Removing this "/" at start of path solve this issue.
You can check this video to see how it works.

On iOS 4 and later, if the file is in PNG format, it is not necessary
to specify the .PNG filename extension. Prior to iOS 4, you must
specify the filename extension.
So the answer is you have to specify the filename extension for all JPEG.
If this not solve your problem, Try this
Instead of using the imageNamed you can load the image with the methods imageWithContentsOfFile
NSString *imageFile = [NSString stringWithFormat:#"%#/iphone4/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image = [UIImage imageWithContentsOfFile:imageFile];
NSString *imageFile1 = [NSString stringWithFormat:#"%#/iphone5/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image1 = [UIImage imageWithContentsOfFile:imageFile1];
NSString *imageFile2 = [NSString stringWithFormat:#"%#/iphone6/%#", [[NSBundle mainBundle] resourcePath], #"1.png"];
UIImage* image2 = [UIImage imageWithContentsOfFile:imageFile2];
I have added 3 folders (with same image name and extension) in my project. I am able to fetch them differently and it work fine

Related

UIImageView displays downloaded images from HTTP, but won't display image resource

In my first View Controller after a user logs in, there is a quick check to see if the user has uploaded a custom image to his account. (If he has, there's a URL in the user's profile data.)
These images are downloaded correctly in my code and they show up beautifully. The problem comes when there is no custom user image, and the VC tries to set the same UIImageView to a default picture (in xcassets). The picture is being loaded into the bundle, as far as I can tell, and it's a valid PNG file.
Here is the snippet for setting the image. If a custom URL is not found, I set the URL parameter string to "nil."
-(void) setImageWithUrl: (NSString *) Url Imageview: (UIImageView *) image {
if (Url.length > 4) {
NSURL *url = [NSURL URLWithString:Url];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *Profileimage = [UIImage imageWithData:data];
[image setImage:Profileimage];
}
else {
UIImage *Profileimage = [UIImage imageNamed:#"DefaultPicture"];
[image setImage:Profileimage];
}
}
This one's driving me nuts, so all weird ideas are welcome. :-)
Let me know if you want me to post any other parts of the code that you think could be a factor.
It looks like debug-need issue. But why don't you use any image download/cache library, like SDWebImage?
It gonna be much efficient and convenient

Image Optimization (iOS)

I'm working on a iPad Magazine and I'm using a lot of images (background, slideshow and animation) and the memory utilized is very high.
I've read the following method uses a lot of memory
UIImage *picture = [UIImage imageNamed:#"myFile.png"];
And they recommended using this one
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/myFile.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
But I've found another method as well
imageView.image = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"png"]];
In order to optimize my app, which method should I use? All my images are .jpg and were saved for web in Photoshop.
All 3 methods will use the same amount of memory. The differences are the following:
Using [UIImage imageNamed:#"myFile.png"] image is cached in memory for faster reuse. This is good for small images used several times in your application (image background, etc). Cache is removed for non used images when memory warning is received.
Using [[UIImage alloc] initWithContentsOfFile:path] image is not cached and you can "force" release of memory by calling [image release] or setting property to nil using ARC. You have a better management of when memory is released
Using [UIImage imageWithContentsOfFile:fullpath] is just equivalent to [[[UIImage alloc] initWithContentsOfFile:path]autorelease]

Images don't display in device but in simulator

I have an array:
langArray = [[NSMutableArray alloc]initWithObjects:#"Italiano",#"English",#"Francais", nil];`
and cell image like this :
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"language_%#_disabled.png",[langArray objectAtIndex:indexPath.row]]];
but images don's display in device, in simulator they display normally.
Check the images name in your Xcode Project..
Because simulator is Case Insensitive and Device is Case Sensitive..
May be the ImageName in your array different Imagename in your local. You can check upcapercase
Check in your Xcode project source may be your image extension is
".PNG" rathere then ".png"

How can I download images into the app? [duplicate]

This question already has answers here:
iOS download and save image inside app
(11 answers)
Closed 10 years ago.
How can I download images into the app? Like I want to fetch images from my website and download it into the person's iphone app to be shown in the app? Basically the image won't be shown by url.
More specific:
How do I download the image to the app without using UIImage. I want to fetch the image and download it as the file name "anne.png" then reference it throughout the app using UIImage as anne.png. See - I want to download it first so that when someone visits the app a second time, they see the image, and see a default image in the meantime.. Thanks.?
http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
URL to Remote Image
We start by creating a URL to the remote resource:
NSURL *url = [NSURL URLWithString: #"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
Create UIImage from NSData
The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
Putting it Together
Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:
NSURL *url = [NSURL URLWithString:#"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
For single image you can use the following. Keep in mind this will block the UI till the image is fully downloaded.
[UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
To download the image without blocking the UI:
dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);
dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});
To save the image to the phone camera roll you can use UIImageWriteToSavedPhotosAlbum.
To save the image in the app's directory. Use NSData's writeToFile:atomically:
To download several images from a website maybe this can help you:
What's the best way to download multiple images and display multiple UIImageView?

relative Path for Ipad Project

I know that this is a stupip question, but i cannot find out what my relative Path is... :(
I have an iPad Project and under this a folder for Images
I want to set an UIImage, but i cannot figure out what my relative path is--
I tried this:
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"/Images/send.png"];
But it is not working....
Best regards
You don't have to find the relative path, if your image is present in your bundle. However [[NSBundle mainBundle] bundlePath] will give the relative path of your bundle.
UIImage *image = [UIImage imageNamed:#"send.png"];
This is basic method for loading the image.
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"send.png"];
This will load the images faster. Because it load the image without cache.
UIImage *image = [UIImage imageNamed:#"send"];
Use this if you want to use the image for both ordinary and retina screens. In your bundle you should have two images "send.png" and "send#2x.png" where "send#2x.png" is twice as the size of "send.png".

Resources