How to access images in library project from main project in iphone? - iphone

I have created one library project in iphone and my images folder is present in my main project. How do i access that images?

You can access the app's resources using NSBundle's methods:
NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];

You can access the resources from NSBundle class.
If your resources is in current application executable, at that time you need to use "[NSBundle mainBundle]".
If you have main bundles in your application, we have a property in NSBundle called "allbundles". It will returns an array of all the application’s non-framework bundles.So that, you can call your resources from any of the bundle.
I think this may useful for you.

Related

Images not shown in my iphone web app

I'm creating a web app and have a problem. My images is shown in my Safari browser and in the Iphone simulator, but they are not shown on my Iphone.
Any ideas?
If HTML files and images are stored locally on device, you have to keep in mid that iOS filesystem is case sensitive. so iPhone.jpg and iphone.jpg can be two different files... make sure that all your tags and corresponding files have correct case...
Also when loading uiwebview and resources for it are in bundle folder you have to make sure that you set baseURL so that webkit can find relative linked files:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
this enables you to link relative to bundle:
<img src=iPhone.jpg">

Renaming the images in ipad and then saving that image to app bundle?

I am creating an app in which i am displaying images from photo library and app bundle by clicking on two separate action buttons.
Now what i want is that i want to create a new action button and its purpose will be to select an image from photo library and then save that image into my app bundle.
Can anyone guide me to right direction regarding this topic.
Thanks,
Christy
I don't think you can modify the application bundle once it's on the iPhone. The entire thing is code signed, and changing it would cause it to not run any more. You can try saving the image to documents directory.
So far as I know you cannot repackage the bundles on the iPhone once your app has been released to the App Store. So go the other way, and put the data from the bundle on the filesystem so you can change it at runtime.
My usual technique for this stuff is:
bundle up the initial data
have a routine that checks for the
presence of a versioned file on the
iPhone's filesystem at startup
if that routine doesn't find the
current version of the file, copy
all the data into the iPhone's
filesystem
reference the data from the
filesystem in my app, rather than
using the bundle path
So, essentially your bundle is just a delivery mechanism, a way to preload the filesystem with the stuff you are going to need. Once it's on the filesystem you can change anything you wish.
References
Downloading image into bundle?
How to save a UIImage to the application's Bundle?
UPDATE
- (IBAction)saveImage:(UIImage *)image {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(image);// Change according to your needs
[imageData writeToFile:savedImagePath atomically:NO];
}
You can make an NSData from the image you are picking from your photo library by using this -
NSData *imageData = UIImageJPEGRepresentation (UIImage *image,CGFloat compressionQuality);
then call
[imageData writeToFile:imgPath atomically:YES];
Here imgPath is the path for TMP dirextory where you want to write the file, get it as -
NSString *filename = #"a.png";
NSString *uniquePath = [TMP stringByAppendingPathComponent:filename];
and TMP is an enum
#define TMP NSTemporaryDirectory()

iPhone dev: how to load images named the same from different groups

I am working on an iPhone project, and have two themes defined under different directories, with most files share the same names (like background.png, image1.png, image2.png, etc). I have had them imported into xcode under different group names (group1, group2).
How do I tell UIImage to load an image from a given group, say, I want group1:background.png?
Thanks,
Tim
Try this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png" inDirectory:#"group1"];
[UIImage imageWithContentsOfFile: path];

iphone: relative paths to local file?

Using MontoTouch for .NET C# iPhone development. (though should not matter)
In the iPhoneSimulator, I use UIImage.FromFile (#"images/Bahai.png"); to get the Bahai.png from the images folder.
However, when I run it in debug mode on my iTouch, the function returns a null.
Only if I put the image file in the root does it work in the iTouch.
Is there a different relative path I need to use?
Ian
Not sure if this will help, but in objective-C, I would load an image from the app bundle like this:
UIImage *image = [UIImaged imageNamed:#"Bahai.png"];
For other types of files, I would get the absolute path like this:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Bahai" ofType:#"png"];

How to load an image stored in a static library

in my iphone app, I am linking with a static library containing objective c files and images. Is it possible to load an image from a static library? I have tried
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
but obviously the image is not in the main bundle, it's in the static library and the NSBundle class seems only to offer access to the main bundle and to bundles with known paths. Is there a way to load an image from a static library on the iPhone?
The simplest way is to store the image in an inline character array:
const char imageData[] = { 0x00, 0x01, 0xFF, ... };
And later when you need it:
UIImage *image = [UIImage imageWithData:[NSData dataWithBytesNoCopy:imageData length:sizeof(imageData) freeWhenDone:NO]];
You will have to convert your image's binary data (after saved as either PNG or JPEG) to the character array manually (or write a script to do so)
It's not clear what you mean by "the image is [...] in the static library". Static libraries are plain files (with the .a extension) and contain archived object files. Bundles on the other hand are directory hierarchies (containing executables and other resources).
If you link a static library, the code from the library is included directly into your executable. No files are copied to the application bundle, so there's no way to copy the image.
If you have the image file with your static library, you can simply copy it to your application bundle by adding a copy files build phase to your target in Xcode.
Do you use an Absolute Path in the static library copy files build phase and point it to the client application? I tried something like this and the images still cannot be accessed.