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

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.

Related

Not able to get images from a link to imageview

In my Iphone app.i'm not getting the images in imageview.Those images are coming from a link.
link is http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg
i'm using this code.
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Try this :-
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Hope it helps you
try this code...
NSURL *imgURL = [NSURL URLWithString:#"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgURL];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
yourImageView.image = tmpImage;
You should make sure the [NSData dataWithContentsOfURL: [NSURL URLWithString:str]] returns not nil . if nil you should use dataWithContentsOfURL:options:error:.
quote Apple Documentation :
Return Value
A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.
Discussion
If you need to know what was the reason for failure, use dataWithContentsOfURL:options:error:.
I've tried the following and it is working fine for me:
NSString *str = #"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg";
//NSLog(#"Image data %#", [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]);
UIImageView *imgv = [[[UIImageView alloc] initWithFrame: CGRectMake(50.0, 50.0, 100.0, 100.0)] autorelease];
imgv.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
imgv.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgv];

How to show Image with space in its name in objective c

I want to show image in imageview from Url and it is having space in its nameā€¦
// NSString* imageURL = #"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/GMT-July-cover1344489425.jpg";
NSString* imageURL = #"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/Sookie Stackhouse small1343970416.jpg";
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[ImageWithspace setImage:image];
but no image is shown bcoz uncommented imageURL contains space in imagename, if i use commented imageURL than it display image in imageview :( ....give me some solution
You need to encode the url:
NSString *encoded = [imageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
You need to encode the URL before you use it to create the NsURL. There is a built in class that will help you encode the string.
Here is an example of encoding a string for use with NSURL: NSURL Encoding in ObjC
NSURL *url = [[NSURL alloc] initWithString:[myurlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
This happens because the white spaces in the url are not treated hence percent encoding is used
NSString* imageURL = #"http://dev.squealrs.com/imaging.php/image-name.jpg?height=45&noimg=100&image=/wp-content/uploads/sqbrands/Sookie Stackhouse small1343970416.jpg";
NSString *spaceUrl = [[NSString stringWithFormat:imageURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
spaceUrl = [spaceUrl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:spaceUrl]];
UIImage* image = [[UIImage alloc] initWithData:imageData]
[ImageWithspace setImage:image];
Just Copy paste this code and see. IF you have problem still then feel free to ask

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];

NSURL code to display an image from a sqlite table

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.

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....