Automatically selecting ipad #2x retina images for ipad3 from document folder - objective-c

I am developing an iPad3/iPad2 application. In my bundle I have both iPad2 and #2xiPad3 images.It is working fine when i use bundle. In iPad2 low resolution images are detected and in iPad 3 high resolution ones are detected. But my problem is when I access it from document folder. How can i do the same for document folder. When I use document folder instead of bundle only low resolution images are loaded. Here is my code.
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *fileName = #"image.png";
NSString *path = [docsDirectory stringByAppendingPathComponentNSString stringWithFormat"/%#",fileName]];
I have placed image#2x~ipad.png also in the document directory. But it is not getting detected automatically in ipad3. I tried using image name without extension also. But the problem persist.

in side your document folder no need to set image#2x~ipad.png. just set image#2x.png is enough.

Related

App is showing up in iTunes file sharing but documents are not (they have previously)

I've enabled file sharing in my app and it works as far as when I go to file sharing in iTunes the app icon shows up. However none of the documents do.
This is especially strange due to the fact that at one point they did and now suddenly they don't. The file is saved in to the documents directory and it saves in to my app and can be loaded no problem within the app itself.
My code to save to documents is as follows:
NSArray * pathsFree = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * freeDocumentsDirectory = [pathsFree objectAtIndex:0];
NSString *freeHipHopFilePath = [freeDocumentsDirectory stringByAppendingPathComponent:#"FreeHipHopMixerOutput.m4a"];
I have the bundle display name set to ${PRODUCT_NAME} but from what i've seen in the more recent questions this is not even a necessity any more to get file sharing to work.
I'm using an iphone 5c running iOS 10.3.2 and the version of itunes is 12.6.1 and finally the version of xcode is 8.3.3. Is a combination of these causing some sort of problem?
I'm at a complete loss as it doesn't seem like anyone else has encountered this problem.
Any ideas?

What is the application Bundle and Resources folder

My question is about the application bundle in a project. I was reading about that and can understand some basic things (I'm not a native english speaker). I can understand that the resources folder is used to hold the files that will be used in the project, e.g. media files (images, audio, video, etc.) and should be in the application bundle to be identified.
So, what is the point if I want to use images and another resources in my project? In my other related question, I can't use them by referencing with NSImage imageNamed:.
I have used the following with no success loading my files:
NSBundle methods
imageNamed:#"string" with/without file extension
the images are in resources folder
I'm learning Cocoa and Objective-C, and of course this is different to C++ or Java when I want to create an ImageIcon or a QImage.
I may not have completely understood the issue, so correct me if I am wrong. I believe the problem has to do with your image's target membership or how you're retrieving the image in your code.
Adding an image to your project target will appropriately copy the resource at compile time. To add the image to your target, select it in the file navigator and then reveal the Utilities Panel. On the Utilities Panel, select the File Inspector Tab. Look for the Target Membership section and ensure that the image is selected for the desired targets:
Do you mean that you can't use the NSImage imageNamed: method to retrieve resources? If so, you can retrieve the resource like this (from the main resource bundle):
NSString *imageName = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png"];
NSImage *imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
It also looks like you already have a good answer to your other related question.

save image from screen to my assets?

This might be a hard thing to do , but
I have to save whats appears on the screen in some moment, to a file.
but not just a file, i need to save it to my assets folder:
my goal is that i can later use the image without the:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
but just to use it like it was on my assets, just to use: #"myImage.png" , when myImage is the image that i have already saved from my print screen .
any direction on how to do that ?
thanks .
AFAIK, you can't save it to app bundle.
I suggest you to write your own methods to load an image like [UIImage imageNamedCustom:...].
It would be nice if this method will search image in assets first, if not found try to load from Documents or Caches. Also, it may cache path to saved images. After that you can replace all occurences of "[UIImage imageNamed:" to "[UIImage imageNamedCustom:" and other similar methods.
Your app's main bundle is part of the app, and is read-only. You can't add or remove content from it on the device. You're going to have to save that image to one of the directories in your app's sandbox, like Documents or Library/Caches.
Instead of +[UIImage imagedNamed:], perhaps +[UIImage imageWithContentsOfFile:] would work for you? 
What you are saying, if I understand correctly, is that you want to save part of a drawing on your screen in an image (which is easy) and want to retrieve that later from your application bundle (I presume you mean that instead of the term assets). This can not be done, because the application bundle is read-only at runtime.

how to get the full path of an ios application? [duplicate]

I have a problem accessing my files in my app.
I am currently using
//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Tiles"];
to access my tiles for my MKOverlay. This gives me this directory
/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles
The x's is only for privacy reasons
I have my tiles in a folder called Tiles in the root of my application which is in Xcode in a group called Tiles which is in directly in the Resources group.
When I run my app, I get a simple error saying that it could not find my tiles at the generated directory (the one quote above) If I replace that piece of code and make it:
NSString *tileDirectory = #"/Users/xxxx/Documents/xxxx/Tiles";
Then my app works fine. This is obviously because it finds my tiles in its direct location on my Mac. This is fine for testing, but I need it to work on my iPhone/iPad.
This problem might be occurring due to:
The generated directory is incorrect.
The tile images aren't getting included in the builded .app file.
Either way, I have no clue of what to do to solve it.
How can I solve this problem?
[EDIT]
I changed that piece of code to:
NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];
Now it works in simulator, because all files are in the apps root folder and I don't ask for it to enter another directory called "Tiles".
This runs with no error on the simulator, but when on my iPhone it gives the original error (just a different file path but also ending with /xxxx.app
How can I ensure a directory in my app file such as xxxx.app/Tiles - TileMap does this.
Since it is your files in your app bundle, I think you can use pathForResource:ofType: to get the full pathname of your file.
Here is an example:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"your_file_name"
ofType:#"the_file_extension"];
Remember that the "folders/groups" you make in xcode, those which are yellowish are not reflected as real folders in your iPhone app. They are just there to structure your XCode project. You can nest as many yellow group as you want and they still only serve the purpose of organizing code in XCode.
EDIT
Make a folder outside of XCode then drag it over, and select "Create folder references for any added folders" instead of "Create groups for any added folders" in the popup.
If your tiles are not in your bundle, either copied from the bundle or downloaded from the internet you can get the directory like this
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:#"xxxx/Tiles"];
NSLog(#"Tile Directory: %#", tileDirectory);
You need to use the URL for the link, such as this:
NSURL *path = [[NSBundle mainBundle] URLForResource:#"imagename" withExtension:#"jpg"];
It will give you a proper URL ref.
You need to add your tiles into your resource bundle. I mean add all those files to your project make sure to copy all files to project directory option checked.

<newbie> Objective C, saving a file in iPad apps

I am creating a sample application where it downloads 100 images from
server and stores it in iPad.
In XCode, I am using the NSURL to retrieve the image file and using
NSData to save it into my local folder.
I am able to save the images in my mac air desktop folder. But I want
my application to be deployed in iPad.
So here is my question:
Where do I store the file in my iPad, so that my application can
retrieve the images when needed?
If possible can someone give me the code for saving the images in
your iPad resource directory (or whatever directory needed). Just the
code where you build the path will do good.
I know this is kind of a basic question, since I am new to objective-C, I am kind of struggling with it.
Here is an example function how to retrieve the applications documents folder. In there you can create your own folder structure. This folder is also backed up by iTunes and will be preserved when doing application updates.
NSString* GetApplicationDocumentsDirectory() {
static NSString* documentsDirectory = nil;
if (documentsDirectory == nil) {
documentsDirectory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0] retain];
}
return documentsDirectory;
}
In my humble opinion, using a Core Data store would be the best approach for this.