NSURL code to display an image from a sqlite table - iphone

Ii'm using this NSURL code to display an image. what I want to do is to insert a url from my database table in place of the static url shown in the code below:
Does anyone know how I should proceed?
NSURL *url = [NSURL URLWithString:#"http://www.nyapplecountry.com/images/photosvarieties/redrome04.jpg"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
[self.containerView addSubview:self.mainView];
I was thinking of something like:
NSString *urlAddress = [NSString stringWithFormat:#"%#", place.url];
where place.url is coming from my db table. I'm missing something though, not sure what.
thanks for any help

Is place.url a NSString object and the full URL to the image? Then it would be as simple as
NSURL *url = [NSURL URLWithString:place.url];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

You can't do it that way. You have to read image from data base and initialize UIImage with that data.

Related

Why won't this image show up? Using NSData and initWithData

NSURL* urlEx = [NSURL URLWithString:#"Pacific_Map.png"];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];
UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];
mapImageViewEx.image = imgEx;
If I replace mapImageViewEx.image = imgEx; with mapImageViewEx.image = [UIImage imageNamed:#"Pacific_Map.png"]; it works, but I do not want to use imageNamed.
That's because imageNamed knows where to scan for the file, but initWithContentsOfURL expects the full path. use
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"Pacific_Map" ofType:#"png"];
NSURL* urlEx = [NSURL fileURLWithPath:imagePath];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];
UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];
mapImageViewEx.image = imgEx;
What's wrong with using imageNamed? Anyway, you could use initWithContentsofPath.
+ (UIImage *)imageWithContentsOfFile:(NSString *)path
Tell us more about what you are trying to accomplish. Good luck,
James
EDIT: Sorry, I assumed Pacific_Map.png was just a placeholder path. Like someone else posted, you need to indicate the full path if you're not going to use imageNamed.

Load image from server on a UIImageView in phone

I'm having a problem loading a remote image into a UIImageVIew...
It just doesn't show the image, may be i'm missing something...
I also use the described here but with the same results
How to load image from remote server on the UIImageView in iphone?
Can someone help me?
This is the code i'm using
Im getting the data from a xml and on the image element I have the full path
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#", [[promoList objectAtIndex: promoIndex] objectForKey: #"image"]] ]]] ];
With this code the image are displayed correctly
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: #"http://localhost/promos/preview/1.jpeg"]] ]];
Since the bottom example works as expected and besides the url string the example looks the same the problem should be with getting the url string. The problem is also that the code looks the same but it is quite hard to see. I would refactor the code to something like:
NSObject *urlObject = [[promoList objectAtIndex:promoIndex] objectForKey:#"image"];
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
[[detailViewController detailImage] setImage:image];
Then you can debug the statement properly and make sure that you get what you expect after each step.
Update
To remove newline and tabs you can:
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
If urlObject already is a string, which it should be, then you can do:
NSString *urlString = [urlObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
which would make it a bit cleaner.
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:#"http://...."]]] retain];
if (img != nil)
{
[imageViewName setImage:img];
[img release];
}

How do you set a UIImage using an absolute URL?

I am trying to set the UIImage of a tableview cell to an absolute url, here's the code i have already:
NSString * imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"images"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//set image
cell.imageView.image = img;
But for some reason this returns null as if the URL supplied is invalid. However i know this is not the case because i can make it work by typing one of the URLs as a string literal into the imageURL pointer and i know the code [[stories objectAtIndex:indexPath.row] objectForKey:#"images"] returns correct URLs as i printed the output to the log and it was fine.
Is there any reason why this would be happening that I'm clearly not seeing here?
You can use the absoluteURL method of NSURL and voila!
NSURL *url = [[NSURL URLWithString:imageURL] absoluteURL];

how can i display image from url?

I have a string variable tmpImgURLStr which contains URL like www.abc.com/img.png. i want to display that image in my imageView for that i have use some code but it's not working which is given below:
NSLog(#"Img URL === %#",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];
As far as I can tell from your url - you have pdf, not an image. Usually WebViews are used for displaying this sort of data.
Update
Your NSData initiation is kinda too long. You can actually initiate a URL without supplying formatted string:
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]];
Also I've noticed that your URL is without protocol. You may want to try adding http:// or https:// to it and then see what happens. And just in case check if your logoImg is actually wired to the NSImageView in your NIB.
Try this
NSURL *imageurl = [NSURL URLWithString:#"http://www.chakrainteractive.com/mob/ImageUpoad/pic2-2.png"];
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
UIImage *image = [UIImage imageWithData: imagedata];
[logoImg setImage: image];
Try this code instead of yours .. May be it will work..
logoImg=[[IUImageView alloc]initWithFrame:CGRectMake(10,10,300,460)];
NSLog(#"Img URL === %#",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];
-Happy coding....

Show image from url in uiimageview

I try to show a image in my iphone app but it doesn't show. I connect in IB and I check to show a local image, it workd good. I also check the url from image and it is ok. I use the next code:
NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
and
//Video.h
NSString *urlThumbnail;
and this is my code on the view.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"URLTHUMBNAIL: %#", aVideo.urlThumbnail);
NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
}
NSLog getts on console
URLTHUMBNAIL: http://147.83.74.180/thumbnails/56.jpg
The connections on IB are ok, cause I can display a local image using next code on the same IBAction
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"barret" ofType:#"png"]];
It can be very helpful to NSLog() yourself some notes about what's really in your variables. If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. I'd like to see that value in the console right before creating your NSURL with it.
First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. Otherwise, your UI may become unresponsive.
You should consider adding some additional logging to check the value of imageData and image. Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure.