NSBundle mainBundle pathForResource: Not working unless #2x included - ios

I have files named: landing_load1#2x ...landing_load4#2x in my project. (No non-retina files)
This code works:
for(int x=1; x < 5; x++){
NSString * imageTitle = [NSString stringWithFormat:#"landing_load%i#2x", x];
UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:#"png"]];
[loadingImages addObject:loadingImage];
}
But this doesn't work:
for(int x=1; x < 5; x++){
NSString * imageTitle = [NSString stringWithFormat:#"landing_load%i", x];
UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]];
[loadingImages addObject:loadingImage];
}
And this doesn't work:
for(int x=1; x < 5; x++){
NSString * imageTitle = [NSString stringWithFormat:#"landing_load%i", x];
UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]];
[loadingImages addObject:loadingImage];
}
Question: I know I don't have to explicitly call #2x or specify the file type, so any ideas why it's not working unless I explicitly write the whole file name out?
Note: Tested on iPad 4

Record
NSString * imageTitle = [NSString stringWithFormat:#"landing_load%i", x];
means that you have a pair of graphics files for any resource - both "filename.png" and "filename#2x.png".
Or, alternatively, it can be used as "alias" for only "filename.png"
If you have only "filename#2x.png", you have two possible variants:
1. Use something like that
NSString * imageTitle = [NSString stringWithFormat:"landing_load%i#2x", x];
or have both of files ("filename.png" and "filename#2x.png") for any resource.

In the 'non' working example you haven't stated nil in the ofType parameter.
UIImage * loadingImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageTitle ofType:nil]];
change to png, should do the trick :)

Because you are using
-[NSBundle pathForResource:ofType:]
method, you need to specify resource type in second parameter, which you were provided as "png" in the code you say it works.

The #2x is handled as part of [UIImage imageNamed:] not as a part of [NSBundle pathForResource:ofType:] Just use the former and you should be good to go.

Related

Can't find the new image in the blue reference folder

Now my app has a blue reference folder, and the app can load images.\
The code is below:
NSString *bundelPath = [[NSBundle mainBundle] pathForResource:#"YSPSdk" ofType:#"bundle"];
NSBundle *sdkBundle = [NSBundle bundleWithPath:bundelPath];
for (int i = 1; i <= 72; i++)
{
NSString *imageName = [NSString stringWithFormat:#"adapte_animation_%03d#3x",i];
NSString *imagePath = [sdkBundle pathForResource:imageName ofType:#"png" inDirectory:#"Loading"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[_imageArray addObject:image];
}
The app builds and runs well.
Then I add some new images into this folder, and change the code as below:
for (int i = 1; i <= 12; i++)
{
NSString *imageName = [NSString stringWithFormat:#"animation_%03d",i];
NSString *imagePath = [sdkBundle pathForResource:imageName ofType:#"png" inDirectory:#"Loading"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[_imageArray addObject:image];
}
The app will crash.
Then I debugged this app and set a breakpoint in the code.
I see the image not be created.
Why?
I must replace the old image with the new image.
What is a remedy in this case?
==============
I rename some old Images and edit the code to load this new name old images.
I run the app, the app shows me the same error:
It can't find the imagePath!
In my main project. TARGETS -> Build Phases -> Copy Bundle Resources.
I delete the YSPSdk.bundle and add it again. I solve the problem.

My device can't show image array, but has no trouble finding the seperate images

In one of the views of my app, i want to display different animations made from a number of jpg images with the following code...
for (i = 0; i <= HighPic; i = i+1) {
[self.imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#%i", imageName, i] ofType:#"jpg"]]];
}
float imageCount = [[[self.selfContent objectAtIndex:overPath] valueForKey:#"lastImageNumber"] floatValue]*2-1;
imageView.animationImages = self.imageArray;
[imageView setAnimationRepeatCount:0];
imageView.animationDuration = (imageCount/25);
[imageView startAnimating];
This works fine inside the simulator, but when i run it on my device, the self.imageArray is equal null. If i instead write:
[imageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#0", imageName] ofType:#"jpg"]]];
It shows the right picture, even on the device, but just not the animations...
The reason i'm not using the
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%#%i", imageName, i]]];
Is because it takes up too much memory...
Thanks in advance!
I fixed the code myself. When i went to the .h file i saw the imageArray was set to weak so i replaced it with strong and that fixed it :)

how to convert a URL string into an UIImage [duplicate]

This question already has answers here:
iOS: load an image from url
(9 answers)
Closed 8 years ago.
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
in imageName string getting the string as given below., How to convert these url to an image
http://appliicdev.xtechnologies.com/iapp/UploadedImages/5bc77bdd-3eaf-4491-813a-5aedcbfbba41.jpg
Thanks
You can use imageWithData from UIImage. Like,
UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
your coding is fine
change this line
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
into bz ur URL contains /UploadedImages/
NSString *imageName = [NSString stringWithFormat:#"%#/UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
yourimageName.image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
will u need to load the UIImage in quickly` use
Asynchronous FreeLoader
SDWebImage
when I try your image in browser it showing the error so check once path is valid or not otherwise your image name is valid or not
Try this:
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
UIImage *imgObj = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:imageName]]];
_yourImgView.image=imgObj;

UIImage with NSData initWithData is nil

The following code does not seem to load an image.
uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:#"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;
datatmp is nil (0x000000) and
the image does exist.
I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:#"newsicon.png"]; instead.
II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):
NSString *iconPath = [[NSBundle mainBundle] pathForResource:#"newsicon" ofType:#"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];
Loading an image from a file is best accomplished with:
[UIImage imageNamed: "newsicon.png"];

NSmutablearray adding unknown amount of images

I have an array i'm filling with images with a loop
items = [[NSMutableArray alloc] init];
for (int i=0; i<43; i++) {
NSString *filepath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%d",i] ofType:#"png"];
UIImage *image = (UIImage*)[UIImage imageWithContentsOfFile:filepath];
[items addObject:image];
}
it works as long as I set the max amount to match my test images. (in this case 43) if I set the max to a higher number, say 200 it of course crashes cause its trying to add nil objects to the array.
How would I go about being able to add the random number of images with that naming scheme to that array?
I have heard mention of using NSFileManager to add the files to array, is that the better method?

Resources