Displaying array contents with NSString - objective-c

I have loaded an array:
currentBackground=4;
bgImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"mystery_01 320x460"],
[UIImage imageNamed:#"mystery_02 320x460"],
[UIImage imageNamed:#"mystery_03 320x460"],
[UIImage imageNamed:#"mystery_04 320x460"],
[UIImage imageNamed:#"mystery_05 320x460"],
nil];
Now I want to display in a label the file name of the image currently being displayed. I thought:
mainLabel.text = [NSString stringWithFormat:#"bgImages= %#",[bgImages objectAtIndex:currentBackground]];
would work but all I get is hex code . I have a button that scrolls through the images very nicely. But when I try to display the image name all I get is what I believe to be the address where the name resides.

The file name of the image is not stored in your UIImage object. There is no such property in the UIImage class. You'll have to store the names yourself. Perhaps you could store the file names in an array, and use this array both to retrieve the images from file and then to look up the image file names later:
imageFileNames = [[NSArray alloc] initWithObjects:
#"mystery_01 320x460",
#"mystery_02 320x460",
#"mystery_03 320x460",
#"mystery_04 320x460",
#"mystery_05 320x460",
nil];
Retrieve the images (you could use a for-in loop for this instead if you wish):
bgImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:[imageFileNames objectAtIndex:0]],
[UIImage imageNamed:[imageFileNames objectAtIndex:1]],
[UIImage imageNamed:[imageFileNames objectAtIndex:2]],
[UIImage imageNamed:[imageFileNames objectAtIndex:3]],
[UIImage imageNamed:[imageFileNames objectAtIndex:4]],
nil];
Get the file name from your file names array:
mainLabel.text = [NSString stringWithFormat:#"bgImages= %#",[imageFileNames objectAtIndex:currentBackground]];

Using %# in your string format effectively just calls the object's description method. The hex value you are seeing is just the value of the object pointer - this is NSObject's default implementation of the description method.
Note that for an NSString objects, the description method returns... You guessed it - the NSString object itself.
In your case, to display the image names, you have to keep them, as it seems UIImage's description doesn't return the image name.
You can also make a UIImage subclass which remembers the name, but not sure it's worth the hassle.

Related

UIImage initWithContentsOfFile doesn't work

I have question: I want to avoid [UIImage imageNamed:].
So i did:
UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:#"myimage.png"];
controller.productImg.image = prodImg;
[prodImg release];
But now the image is not shown anymore.
Does anyone know how to get that to work?
The above code is not working because correct way to do it is-
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"default" ofType:#"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath];
controller.productImg.image = prodImg;
[prodImg release];
;)

iOS load an image based on user input

fairly new to Objective-C and iOS development (coming from PHP) and I have a relatively simple question that I can't seem to find an answer to:
I am following along with an example for split View design where a web page is loaded into the Detail View when a user clicks an item in the master view. I got all this working, but would like to substitute web view for an image. So I've amended the app to load a UIImage instead of a WebView. What I'm looking for is the equivalent to this code:
NSString *urlString = [pagesAddress objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
// these 2 is where I get lost with the images.
NSURLRequest = *request = [NSURLRequest requestWithURL:url];
[detailViewController.webView loadRequest:request];
I came up with this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
UIImage *myImage = [UIImage imageNamed:imageName];
// missing the last 2 calls: one to tell Xcode that it's an image "request" I want and the second to load the actual image (based on it's name that is already in an array) into the ImageView.
Thanks.
PS
I tried this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath .row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
And it shows just the first image, then crashes when I try to show the last one.
In the end, the solution were those 2 lines when I amended the code:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
Notice that I had to change the setImage to convert the NSString to a UIImage or Xcode would complain. It turns out it was crashing because in the array where I had the image names, I had put 3 images into one entry (basically I forgot the commas!) so it was out of range.
Tim:
This line you gave me
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
is unnecessary because I already have a view created, it would create another view which I never used. Also, replacing it with CGRect seems overkill if I already have a UIImage placeholder no?
In any case, it works now and I'm very grateful for all the help. iPad development with Objectve-C is a very thorny road and I expect I'll be bugging you guys some more.
Cheers.
Try this:
UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithConentsOfURL:[NSURL URLWithString:urlString];
// don't know if you already got the following?
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:myImage];
[self.view addSubview:imageView];
The first line is synchronous (= blocking), so in production, you should rather use - [NSURLRequest start] for this (but that's a bit more complicated).
Or use this for your local images:
UIImage *myImage = [UIImage imageNamed:imageName];
// Now, follow the same steps as in the first code-example, just skip the first line.
Try this (on iOS 4.0 and later):
// Execute a block of code on a background thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
// When IO is done and image created, set it on the main thread.
dispatch_async(dispatch_get_main_queue(),
^(void)
{
imageView.image = image;
});
[pool release];
});

Image on TableView which is retrieved from CoreData

I want to display an image that is being retrieved from core data and every single cell will have a different image. I have done this but I do not know how to retrieve from this path and place the image into the cell. Anybody can help me on this? Thanks in advance.
NSString * filePath = #"01.jpg";
NSData * imageData = UIImageJPEGRepresentation([UIImage imageNamed:filePath], 0.1);
[productInfo setValue:imageData forKey:#"productImage"];
You could fetch the managed object from your model and place it in the array where the array would contain the photo names as below. I am assuming that your entity be photoStore and the column for storing the photo file name be nameOfPhoto, so the code would look like,
NSFetchRequest *fetchRequest=[[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity=[NSEntityDesription entityForName:#"photoStore" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity : entity];
NSArray fecthedObject = [fetchRequest executeFetchRequest:fetchRequest error:nil];
for(NSManagedOect *photos in fetchedObect)
{
[self.arrayOfImageNames addObject : [photos valueForKey :#nameOfPhoto"]];
}
Now, inside the cellForRowAtIndexPath use the image name to add the image to your cell's imageview as,
NSString *imagePath = [NSHomeDirectory() stringByAppendingString : [[self.arrayOfImageNames objectAtIndex:indexpath.row] stringByAppendingPathComponent:"jpg"]];
cell.imageView.image=[UIImage imageNamed:imagePath];
This is how you would add the store the image name into core data and then retrieve the name construct a path to it and display it.
if you want to get path use this -
[[NSBundle mainBundle] pathForResource:#"01" ofType:#"jpg"];

How to display images from plist in cocoa mac

I am developing mac desktop app using objective c. i have array of images to display one after one when button pressed, so i am want to use plist to store image names and i want to display image on image view using plist.
Just load your plist where you stored you image's paths to an array like this :
NSString *myfile = [[NSBundle mainBundle]
pathForResource:#"images" ofType:#"plist"];
imagesArray = [[NSArray alloc] initWithContentsOfFile:myfile];
and then when you want to display an image, juste get it's path from the array above, example :
UIImage *img = [UIImage imageNamed: [imagesArray objectAtIndex:4]];
myImageView.image = img;

parsing txt from array to UILabel

I'm actually starting to loose the will to live, this piece of code is driving me nuts!
I'm trying to get the content of mathspractice.txt into *myLabel
I'm using an array which is:
-(void)loadText
{
NSArray *wordListArray = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#”mathspractice” ofType:#”txt”]
encoding:NSMacOSRomanStringEncoding error:NULL] componentsSeparatedByString:#”\n”]];
self.theMathsPractice = wordListArray;
[wordListArray release];
}
and then I'm trying to pass it into *myLabel
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
myLabel.text = *theMathsPractice;
[myScrollView addSubview:myLabel];
[myLabel release];
}
Can anyone help?
It looks on quick inspection that your theMathsPractice is an NSArray, not an NSString, which is what you'd want to assign to the label's text property. You should at least format that array back into a string of some sort before assigning it to the label.
(Also not sure why you're dereferencing it with the * in the assignment-- I would think that would throw a compiler error, since naked non-reference Objective-C objects are not really allowed.)
I would use the following:
myLable.text = [theMathsPractice componentsJoinedByString:#" "]);