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

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.

Related

Why is NSFileManager unable to find these png files?

I have written code to open image files after studying answers to the questions found here (a, b, c, d, e, f & g). But NSFileManager is unable to find them even though I added the png files to the project. I'm reasonably confident my code should be able to recognise either of the png files if they were in the right directory.
e.g.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSLog(#"\ndirPaths %# \n\ndocsDir \n%#", dirPaths, docsDir);
NSString *imageFilePath = [docsDir stringByAppendingPathComponent:#"iTunesArtwork1024.png"];
NSLog(#"\n\nfile path should be \n\n%# \n\n", imageFilePath);
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
[self.view addSubview:logo];
}
But here is the log in the debug window
2017-07-14 18:26:35.679 IconShape[1089:348564]
dirPaths (
"/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents"
)
docsDir
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents
2017-07-14 18:26:35.679 IconShape[1089:348564]
file path should be
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents/iTunesArtwork1024.png
2017-07-14 18:26:35.679 IconShape[1089:348564]
Image file not found
Here is the state of the project after two png files were added.
Yet the log shows they are not visible to NSFileManager. So where would they be found ? i.e. what changes do I need to make to my code in order to find them ?
EDIT
This draft finds the png file following Subramanian's recommendation.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *imageFilePath = [[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
logo.image = image;
[self.view addSubview:logo];
}
The log now reads
Found file path : … .app/iTunesArtwork1024.png
and the image also appears in the subview.
__
Image is not in theDocument Directory, It's inside the bundle.
You have added the image files inside the project. But You are checking the image on Document Directory, Which is wrong. Image is inside app bundle.
You can simply assign the Image to UIImageView by the name of the image.
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
If you want to get the path of the image, then you have to use [NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
You can access images with names which are added in projects. Try with below code
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
The path where your are looking for the image is inside your device (real or simulator).
To load an image that is stored in your XCode project just do:
UIImage* image = [UIImage imageNamed:#"iTunesArtwork1024.png"];

iOS Objective-C Display PNG Image In Custom Framework

I'm creating a custom framework. Previously in the framework we would download an image and use that to display in a button:
NSString *path = [NSString stringWithFormat: #"https://s3.amazonaws.com/assets/home.png"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *homeButton = [[UIImage alloc] initWithData:data];
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: #selector(backToRewardsCenter:)];
However, in reviewing this we determined that this isn't good to have as a synchronous call so I'd like to add this PNG to the framework itself.
I've been able to do this by adding the image and ensuring it's included in the copy bundle resources Build Phase. With this set the image gets added in the universal framework output:
However, when I attempt to add this in code, it doesn't seem to show up. Also, when I add the framework in a project, I don't see the image being included, just the headers:
Here's what I've tried so far:
NSString* path = [NSString stringWithFormat:#"%#/TheoremReachSDK.framework/home.png", [[NSBundle mainBundle] bundlePath]];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
NSBundle *bundle = [NSBundle bundleForClass:[TheoremReach class]];
NSString *path = [bundle pathForResource:#"home" ofType:#"png"];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
UIImage *homeButton = [UIImage imageNamed:#"home.png"];
But none of those display anything. Any idea what I need to do to get the image to display?
I'm guessing NSBundle isn't finding the framework via the call to:
[NSBundle bundleForClass:[TheoremReach class]]
Try giving your framework an explicit bundle ID, e.g.: com.theoremreach.sdk, clean your projects and then rebuild.
You can then use code like this to fetch and display your image:
NSString *bundleIdentifier = #"com.theoremreach.sdk";
NSBundle *bundle = [NSBundle bundleWithIdentifier:bundleIdentifier];
if(bundle != nil) {
NSString *path = [bundle pathForResource:#"home" ofType:#"png"];
UIImage *homeButtonImage = [[UIImage alloc] initWithContentsOfFile:path];
if(homeButtonImage != nil) {
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: #selector(backToRewardsCenter:)];
} else {
NSLog(#"couldn't find home button image in bundle");
}
} else {
NSLog(#"could not find bundle with identifier %#", bundleIdentifier);
}

Unable to read Images from xcode project in iPad IOS 10.2.1 64bit when download app from app store (testflight)

Unable to read Images from xcode project for example.
drag and drop one image to xcode project, then read using following code.
__weak NSString *strTemp = [NSString stringWithFormat:#"%#/s_%#",[[NSBundle mainBundle] resourcePath],[[self.arr_gallery objectAtIndex:i]valueForKey:#"filename_larger"]];
strTemp = [strTemp substringToIndex:[strTemp length]-3];
strTemp = [NSString stringWithFormat:#"%#png",strTemp];
// NSString *strTemp = [[NSString alloc] initWithFormat:#"%#/s_BGP001#2x.png",[[NSBundle mainBundle] resourcePath]];
UIImage *image;
image = [[UIImage alloc] initWithContentsOfFile:strTemp];
img.image = image;
strTemp = nil;
Write following code.
NSString *newStrTemp = [strTemp stringByReplacingOccurrencesOfString:#"jpg" withString:#"png"];
Instead of this one.
strTemp = [strTemp substringToIndex:[strTemp length]-3];
strTemp = [NSString stringWithFormat:#"%#png",strTemp];

NSBundle mainBundle pathForResource: Not working unless #2x included

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.

How to separate images out from Resources in iOS

I'm displaying a set of images in my app. I've got the code working when I load the image from the Resources folder, like so:
- (void)viewDidLoad
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: #"pic_a"]];
[array addObject:[NSString stringWithFormat: #"pic_b"]];
[array addObject:[NSString stringWithFormat: #"pic_c"]];
[array addObject:[NSString stringWithFormat: #"pic_d"]];
int index = arc4random() % [array count];
NSString *pictureName = [array objectAtIndex:index];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:#"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
However, if I create an "images" group within the "Resources" group, and put try to load the images from there, the image within the view shows up blank.
[array addObject:[NSString stringWithFormat: #"images/pic_a"]];
[array addObject:[NSString stringWithFormat: #"images/pic_b"]];
[array addObject:[NSString stringWithFormat: #"images/pic_c"]];
[array addObject:[NSString stringWithFormat: #"images/pic_d"]];
I'd like to separate the images out from the nib files and all the other cruft. What's the best way to do that?
Even if you have the images in a separate group within Resources, you can load them by calling the name, e.g. use this one line
UIImage *img = [UIImage imageNamed:[array objectAtIndex:index]];
in place of these three lines:
NSString *pictureName = [array objectAtIndex:index];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:#"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
You will still fill the array simply by
[array addObject:[NSString stringWithFormat: #"pic_a"]];
If you have both jpg and png files, then you should append .png to the end of the file names. Otherwise, leaving it off is fine.
Try using the imageNamed: method instead of imageWithContentsOfFile:
You need to create a physical folder called images (it should have a blue color instead of the usual yellow color)
Remember that adding groups inside your project it is simply for organization and look within Xcode. You adding a group will not change the fact that the images will be saved in the main bundle. If you are trying to find them using the images group in the string it will not find them.
There are some things you can do to make this work, but in reality you don't need to.

Resources