image downloading from URL in iPhone application - iphone

I am using google chart functionality in iPhone app....I want to download image and put it into UIImageView from http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World ...
Is there any sample app for doing such functionality?or any other way to display charts in app?
Thanks in advance...

This would block, use NSURLConnection for non-blocking download
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
myImageView.image = downloadedImage;

Try imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]]

#iSwap - I know that why your code is not working.
i think you put an url as a string instead of url.
as above code ----
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
there is aURL that should be a url not a string, and u wrote it as a string like
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
you have been forget to convert this string into an url.
the right method will be like this-
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]]];
i hope you got it, and one more thing there is no difference between "willcodejavaforfood" and "CodeBrickie" code. just way of writing is little differ.

Related

Can't set UIImage to UIImageView from Url?

I can't understand why I can't set the uimageview from url from the example below:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://a4.mzstatic.com/us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"]];
UIImage *image = [UIImage imageWithData:data];
self.imgAlbum1 = [[UIImageView alloc] initWithImage:image];
it works fine by browsing the image, IBOutlet is ok, even setting uiimage from imagenamed doesn't work
I tried to recreate the contorl in the storyboard but nothing...
any tips are appreciated
Presumably imgAlbum1 is an outlet to your existing image view.
Instead of that last line, which tries to create a new image view without displaying it anywhere, you probably want:
self.imgAlbum1.image = image;
Edit: If your imgAlbum1 is an IBOutlet, you probably set it as a WEAK property. If that is the case, the image will never show up. In order to use alloc, your property would have to be a STRONG property.
Since you are probably using a WEAK outlet, try using self.imgAlbum1 as the other poster suggested. The reason this will work though, is because you're just updating the image, not the actual imageView object (weak properties, when you re-alloc will essentially destroy the object completely).
This is what I did which I tested and works just fine:
NSString *encodedString = [[NSString stringWithFormat:#"%#", #"us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://a4.mzstatic.com/%#", encodedString]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imgAlbum1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgAlbum1];
So the questions for me are:
Are you using a WEAK property for imgAlbum1 (ie: #property (nonatomic, weak) IBOutlet *imgAlbum1)? If so, don't set it to strong - rather, just use self.imgAlbum1.image = image;
Have you ensured you have an internet connection with the device you are using?
Is imgAlbum1 an IBOutlet to a UIImageView ? If so, is it hidden (or hidden by a layer)?
Are you on the right view controller?
etc.
Otherwise, the code seems fine.

iphone sdk how to add uitableview cell image dynamically?

I am new to this iPhone development. Am developing a catalogue application. In that i need to give all product details with product images as tableview cell image dynamically since my images are coming from web service. I did this,
NSString* imageName = [NSString stringWithFormat:#"%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]];
cell.imageView.image.size.width==20;
cell.imageView.image.size.height==20;
cell.imageView.image = [UIImage imageNamed:imageName];
NSLog(#"imagename:%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]);
Am getting all the images in my console window and i checked that using nslog. But am not getting the images with simulator.What am doing wrong in this? Can anyone of you help me out?
Thanks in advance.
UIImage imageNamed: takes an NSString so if you want to use a formatted string like that use NSString's stringWithFormat
try:
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",currentLink]];
As you are new to iPhone development I'll just expand that out to make it clearer (Ignore this if you already got what was happening there)
NSString* imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];
Do as follows:
NSString *imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];
Apple documentation contains an example of putting downloaded images into table cells. It's called LazyTableImages and would probably help with what you're trying to do.

Loading an image from multidimensional array in to UIImageView

I'm loading an image to a custom cell and I build up an array throughout my code from XML which I want to set to my UIImageView on my nib file.
My array gets built up to totalArray and then I do the following in my cellForRowAtIndexPathT:
newObj1=[totalArray objectAtIndex:indexPath.row];
aCell.lblRouteText.text = newObj1.routeText;
And the following to load my image works (static):
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://imageurl.jpg"]]];
aCell.imgRoute.image = image;
However, when I try to put my array in using the following:
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[newObj1.routeImage]]];
I get an error of identifier expected
Any tips?
Tom
You're writing [newObj1.routeImage] where you just need newObj1.routeImage, i.e. your final line should probably read:
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newObj1.routeImage]];
When you use square brackets (and not in the context of a C array), you should always have something of the form [x y], which means 'send the message y to the receiver x'.
Note on threading: be careful about using [NSData dataWithContentsOfURL:], because it blocks while the URL contents are fetched. If you call this on the main thread, you will block your UI and the app will be unresponsive.
(Btw, if you're posting error messages, it's best to copy and paste the full actual error line(s) from the console if possible -- it maximises our chance of understanding the problem.)

save a photo library URL as an NSString then back to URL

what i am trying to do is save a URL to one of the photo library pictures. then use this URL to set the image on a CCSprite.
this is the URL being saved as a NSString :
[_currentTarget setObject:[[NSString alloc]initWithString:[[info objectForKey:#"UIImagePickerControllerReferenceURL"]absoluteString]] forKey:#"image1Name"];
Then set the a property of a Target object i made
NSString *image1PathName = [[listOfUserMadeTargets objectAtIndex:num]objectForKey:#"image1Name"];
self.normalImage = image1PathName;
this is when the CCSprite is made
Target *target = [[Target alloc] targetThatIsUserMade];
[_targets addObject:target]; // add it to the array
target.position = ccp(spawnX,spawnY);
target.scale = -0.001875 * target.position.y + 1.09375;
[self addChild:target z:1];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:target.normalImage]]];
[target setTexture:[[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:#"key" ]];
The problem im having is that the Target is there, but the image is not
I have done this many different ways with no luck
thank you for any help
This is a post (and answer) I made which shows you how to save the NSURL for a UIImage in the CameraRoll, and use it later to get the image back, using a UIImagePickerController. This sample code should also help you in how to access the image. Hope that Helps!

UIImage imageNamed

I'm trying to use a url as a UIImage in the OpenFlow API.
NSString *imageUrl = [[[newsEntries objectAtIndex:0] objectForKey: #"still"] retain];
NSURL *url2 = [NSURL URLWithString:imageUrl];
NSData *photoData = [NSData dataWithContentsOfURL:url2];
UIImage *imageUI = [UIImage imageWithData:photoData]
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:myImage]];
[imageUr release];
[(AFOpenFlowView *)self.view setNumberOfImages:3];
I have tried it like this, but no success. The only way I got this API working was using the imageNamed type. The initwithData has no success.
So how can I change this NSString to finally become a imageNamed method?
A UIImageView is different from a UIImage.
Change this line: [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:myImage]];
To this: [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageUI]];
and it should work.
You have several significant errors here. It appears that you may need to read about C/Objective-C and types.
It sounds like you are asserting that, specifically, the line
UIImage *imageUI = [UIImage imageWithData:photoData]
is not working. The code up to that point actually looks okay (though it is not necessary to retain and then release the imageUrl variable.
Once you have created your UIImage, you should be able to pass it directly to your AFOpenFlowView:
[(AFOpenFlowView*)self.view setImage:imageUI forIndex:0];
The line
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
has two errors (and is unnecessary anyway). First, -initWithFrame takes a CGRect as its argument, not a UIImage. UIImageView does have an initialization method -initWithImage, which is probably what you intended. But either way, methods that start with "init" are instance methods, not class methods, so you have to call them on actual instances of a UIImageView, like this:
UIImageView *myImage = [[UIImageView alloc] initWithImage:imageUI];
Note that this will leak memory unless you autorelease or release it.
The following line, you correctly try to give a UIImage to your AFOpenFlowView, but you attempt to create that UIImage by passing a UIImageView to the +imageNamed method. +imageNamed takes an NSString that contains the name of the image, and passing anything else to it won't work.
You must always be aware of what kind of object a method is expecting to receive, and make sure you find some way to give it that kind of thing.
Probably what you are looking for here is something like this:
NSString *imageUrl = [[newsEntries objectAtIndex:0] objectForKey: #"still"];
NSString *imageName = [imageUrl lastPathComponent];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName]];