Get UIImage reference - ios5

i'm a bit stuck with UIImage.
In my app directory i have 3 pictures of different flags (red.png, yellow.png, green.png) which represent different severity levels for alerts. I created a class 'Alerte' in which one of the parameters is an UIImage picked from one of these flags. So i give the reference to an image like this:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"jaune" ofType:#"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
When i print with an NSLog the image the debug area displays:
2012-07-26 11:46:28.546 ApplicationFormulaire[1031:fb03] <UIImage: 0x8144e20>
But how can i know which of the 3 flags is pointed out ? if it's the yellow red or green?
Does it exist a way to get back the name of this resource?
I would be grateful if someone can help with dealing with it :D thanks

I'm not sure but i do not think UIImage keeps information regarding its path.
As an alternative that will allow you to access the correct images would be to store them in a NSDictionary. You could then access each one using [myDict objectForKey:key] to get the red, yellow or green images.

Related

Saving a NSArray that contains a NSDictionary that contains an NSArray of UIImages (and others)

I've seen various questions here pertaining to saving NSArrays/NSDictionaries, but I'm a bit confused about what to do when some of the subelements are UIImages.
To give a little context, the app is essentially a blog-type app. When the user is composing a new entry, their post can contain the following:
Up to 3 images from their photo album
Text
Location
In essence, I'm trying to implement a "Save Draft" functionality to the app if the user decides to temporarily cancel their blog post. When the user cancels the blog post, they will be asked in a UIActionSheet if they would like to save their draft. When the user wants to post again, they can begin from where they left off with their saved draft.
At this point, I would need to save these following objects:
1) NSArray of selected photos
---> contains NSDictionaries (up to 3)
--------> UIImage (large sized version)
--------> UIImage (thumbnail sized version)
2) NSDictionary of NSValues (just some view x,y position data)
3) Text -- NSString data of the blog text they have written
4) Location text -- NString data of their current location
Given that I need to save the above 1~4 data in order to make the "Save Draft" functinality, what is the best way to do this? Should I make a special class to hold all of this data? Also, do I first need to make the UIImages into NSData before I can save them to disk?
Thank you!!
Yes a class/model like structure make more sense and easier to handle as well.
Something like-
Interface Blogdata
NSArray *selectedPhoto;
NSDictionary *positionValues;
NSString *blogText;
NSString *locationText;
and then you can make one more model for photo data;
Interface Photodata
NSDictionary *photo;
UIImage *largeImage;
UIImage *thumbImage;
All of the properties that you mentioned seem like they would belong in a Blog class. Are they already grouped together? A Blog object could capture the state of the draft with variables (properties) of the object being the four things you mentioned. You can then save the Blog object as NSData and read it when the user wants the draft again.
The advantage of this is that you only have to worry about saving one object, instead of having to think about saving four each time (and retrieving them).
The easiest way would be to save the images to the apps documents folder and save the NSArray of filenames and other data that can be represented as text in a drafts.plist.
filenameStr = [NSString stringWithFormat:#"image1.png"];
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
imagePath = [documentsDirectory stringByAppendingString:#"/"];
imagePath = [imagePath stringByAppendingString:filenameStr];
NSData *imageData = UIImagePNGRepresentation(myImage);
[imageData writeToFile:imagePath atomically:YES];

Extracting array of images from url

After doing some research I am a little confused. I can grab a single image from a url if I know the exact directory for example,
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.myUrl.com/pic/workaholics.png"]]];
UIImageView *view1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:view1];
This of coarse works fine, but what if you didn't know the exact path. In Linux you could always do some sort of recursive search but with iOS I can't figure it out. For example, in the "pic" directory that I specified in the latter case what if there was other directories called pic1,pic2,pic3 etc and each directory had an arbitrary number of png's. What I am having a hard time wrapping my head around is how can I recursively search the "pic" directory to extract all directories contents that match .png. I would of coarse then store this is NSData and convert to a string and then store the contents in an array. I'm very much confused at this point. Any help would be greatly appreciated.
Your difficulty here will be getting a directory listing from a web server - since you are retrieving the images over the web. Many web servers will not provide a directory listing as a security measure against hacking attempts.
if you control the web server, you can reconfigure it to provide a directory listing - then make an NSData object from the directory level URL (http://www.myUrl.com/pic/) - and parse this to pull out all the files within the directory. You can then request each individual picture and add it to an array yourself.
if you don't control the web server, but can write to the directory, you could achieve a similar implementation by creating a manifest file (XML-plist would be a good format) which lists the filenames contained in the directory. You'd need to update the manifest any time you added a new picture to the directory though.
otherwise - you have to do some pretty dirty/hacky things, like assuming there are no more than n pictures, all pictures have the filename "pic_m.png" and then iterating through a loop from 0 to n attempting to retrieve each file until you hit a failure.
UIImage needs an explicit thing (URL or filepath) to get its data from.
One potential solution is to implement searching for your .png files via NSFileManager's enumeratorAtPath: method. The reference guide I've linked to even has a code fragment you can use to to create UIImages (just switch the #"doc" to #png and instead of doing scanDocument, do your UIImage * image = ... thing).
I'll leave it as an exercise to figure out how to do it recursively, but I hope my answer helps you out!
NSURL *url = [NSURL
URLWithString:#”www.ArticleDean.com\images\sample.jpg”];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] intiWithData:data];
try this one

how to get array of files of groups in resources in xcode 4

I am new to iphone programming. I have a scenario in which I am creating a group inside a group. that is, in project I have created
Resources->group1
in group 1 I am storing png files and I want to take all those files in one array.
Please suggest me a way to do this.
Not sure for getting all image in an array in one shot , But you could get individual images by their name and add them in NSMutableArray of UIImages
Use below to get a image by name
UIImage* myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:myImage.png ofType:nil]];

dataWithContentsOfFile:filepath of NSData sometimes works, sometimes has no function

I used the codes below to load image file in app bundle.
The codes work! imageData does NOT return 0x0
NSMutableString *sss;
sss=[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[sss appendString:#"/"] ;
[sss appendString:#"thumbnail.png"];
NSData *imageData = [NSData dataWithContentsOfFile:sss];
but the codes below in which almost everything is same have no function, imageData always DOES return 0x0, it looks like [NSData dataWithContentsOfFile:filepath ] does not work for large size image file
NSMutableString *sss;
sss=[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[sss appendString:#"/"] ;
[sss appendString:#"original.png"];
NSData *imageData = [NSData dataWithContentsOfFile:sss];
Both of thumbnail.png and original.png are in the same directory of main bundle.
thumbnail.png
original.png
Welcome any comment
Thanks
Marc
It won't have anything to do with the size of the file. Check and see if you've spelled the file name correctly in your code. The file names are case-sensitive, so if the file is named original.PNG, then you'll need to make sure it is written that way in code as well.
Make sure that the original.png file has been added to your project, and if so, try removing it from your project and re-adding it. When you drag the file into your project, make sure the option "copy files..." is checked.
check out if it really contains on resource .
check that file is in:
Targets/ProductName/Copy Bundle Resources
I also think that the file has not been added to resources. Check the 'Resources' folder in your xcode project, and if it isnt there, put it in there. Either by dragging it in there, or by rightclicking -> new file.

How to Convert data obtained from Sqlite database into NSArray to NSData in iPhone SDK

In My iPhone App,
I am converting my image in to NSData format and storing it into sqlite database Table in dataType="BLOB",
But, I am retriving that image (stored in NSData format) into NSArray Format from database,
The Problem is I am not able to convert NSArray to NSData ( Which would Help me to get my image back into NSData format )
Please Help and Suggest,
Thanks
Your requirement is so odd.
you can store image in local file system with the file name in sqlite
if you really want images store in sqlite, you can every image in sqlite separately rather than archive images in NSArray than convert to NSData
Use NSKeyedArchiver How to convert NSArray to NSData?
UIImage *imgData = [UIImage imageWithData:[yourArray objectAtIndex:theIndex]];
for multiple images in one array you can loop it
like
for (int i = 0;i<[yourArray count];i++) {
UIImage *img = [UIImage imageWithData:[yourArray objectAtIndex:i];
//do something here with each image
// for example, add it to an ImageView
}
P.S. if theres only one image, its index is 0
hope this helps