iPhone: Problem loading images from NSURL - iphone

I have array of image url. I want to show the images into UIImageView.
Now I convert the URL to NSData and then convert that into UIImage and then try to load that into UIImageView.
But it takes a lot of time to do this.
Is there a better way where in I can load the images in a faster and better manner?

Despite all of the answers on here telling you to do this in one line of code, it will sadly make no difference to the URL connection speed OR data / image decoding. If you want a faster way to TYPE the code then fine, but I would use category added to UIImageView....
#interface UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl;
#end
#implementation UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl {
NSURL *url = [NSURL urlWithString:aUrl];
NSData *data = [NSData dataWithContentsOfURL:url]
UIImage *image = [UIImage imageWithData:data];
if(image != nil) {
[self setImage:image];
}
}
#end
Now you can include the header and do...
[myImageView loadFromUrl:#"http://myurl.com/image.jpg"];
For more categories (I will be adding this one to my list!) check here. Those are all my useful ones, you may find them useful too! :)

Use everything in a single statement.
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];

[UIImage imageWithData:[NSData dataWithContentsOfURL:]]

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];

try this:-
image is UIImage and imageView is UIImageView
NSData *receivedData = [NSData dataWithContentsOfURL:#"yoururl"];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
[self.imageView setImage:self.image];

Related

UIImageView does not load image from URL iphone

I have imageView i want to load image from url but it is not loading i am using following code
pdfView.hidden=NO;
webView.hidden=NO;
pictureImageView.hidden=NO;
pdfView.backgroundColor=[UIColor clearColor];
doneButton = [[UIBarButtonItem alloc]initWithTitle:#" Done " style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonClick)];
self.navigationItem.leftBarButtonItem =doneButton;
webView.backgroundColor=[UIColor clearColor];
NSLog(#"File Name is %#",fileName);
NSLog(#"Enterning in the ImageSection");
NSString *ImageURL = fileName;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
pictureImageView.image = [UIImage imageWithData:imageData];
Instead of this if i want to give from local like below then also it does not show image in imageView any idea how to load image from url thank
pictureImageView.image=[UIImage imageNamed:#"starblue.png"];
First set frame of that image and then write this line after add image in pictureImageView
[self.view bringSubviewToFront:pictureImageView];
Code looks fine
Things to check
Url contain a valid image
Set ImageView alloc it properly set frame and then add image
UIImageView *pictureImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 50, 50)];
Then do the rest
This is synchronous image loading that will block the main thread activities.Prefer to use asynchronous image download using AFNetworking or AsynchImageview
Use this code for show image in Url .
NSString *image=[NSString stringWithFormat:#"Your URL"] stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSLog(#"%#",image);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
Yourimageview=[UIImage imageWithData:imageData];
pictureImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]];

how to display image in UIImageView in a specifc block of window in iphone

I have a window in which I want to display an image in a specific block of that window. The image is retrieved from the local XML file. I parsed the XML file and got the image value in the log, but I don't know how to put it in that specific block. The below image pasted for the reference.
The arrow mark represents that within this UIImageView I need to get the image. Also, I doubt that I need to mention any name for reference to UIIMageView in storyboard. Suggest me an idea.
Edited
My parser code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Sample.xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:theParser]
After this I am using this code for getting image:
UIImage *image = [UIImage imageWithContentsOfFile:#"/Users/administrator/Desktop/imageApp/resources/Main_pic1.png"];
After this I don't know how to continue.
Yes, you have to create IBOutlet in .h file
like IBOutlet UIImageView *imgview; and connect imgview with your UIImageview from xib.
After that you can set image to image view.
NSURL *URL = [NSURL URLWithString:[your url which u get form local xml]];
NSData *imageData = [NSData dataWithContentsOfURL:URL];
UIImage *image = [UIImage imageWithData:imageData];
imgview.image=image;
Ttry this, It may help you.
You can directly pass the url image to Uiimageview by :
YourImageView.image = [UIImage imageWithContentsOfURL:YourUrl];
It's possible to do the equivalent by going through NSData, but it's a bad idea. Instead, the image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view.
Save all your urls in an say NSMutableArry *UrlArray then
int x = 0;
int y = 10;
for (int i = 0; i< [UrlArray]; i++)
{
UIImageView *imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(3+x,y, 80,
80)]autorelease];
imageView.backgroundColor = [UIColor blueColor];
if (x >= 300)
{
x = 0;
y = y+imageView.frame.size.height+3;
imageView.frame = CGRectMake(x+3 , y, 80, 80);
}
CIImage *beginImage = [CIImage imageWithContentsOfURL:[UrlArray ObjectAtIndex:i]];
imageView.image = [UIImage imageWithCIImage:beginImage];
x = x+imageView.frame.size.width+5;
[self.view addSubview:imageView];
}
will do the work i guess;

how to get the image from xml file?

I have xml file in that i have
<Image>/9j/4AAQSkZJRgABAgEAYABgAAD/7RguUGhvdG9zaG9wIDMuMAA4QklNA+0KUmVzb2x1dGlvbgAA</Image>
I want image on my simulator..
Is there any way to get it?
it is in byte code but i want byte code into image.
Please give me reply with solution.
You need to convert that nsstring into base 64 encoding string and then
UIImage *image = [UIImage imageWithData:[NSData dataWithBase64EncodedString:64encodedstr]];
You can use
UIImage *img = [[UIImage alloc] initWithData:data];
or
UIImage *img = [UIImage imageWithData:data];

making UIImage from bytes

I have some data that is coming in xml tag from server and is in byte form
i have to convert it to UIImage
I have tried various links on google and also here but no help...
I have used
`NSData *fileData = [NSData dataWithBase64EncodedString:#"/qf20+EWOfpJfjrUaU6n1fSRjXF8ALQZEDy8v7az8Ks8/................SVrttqCVOp9X0kDtYXh2EZIHYGYfVUlptlcpTLtfnn7PR6Tbpl8rpjy93lxcYmDftq9ZSCVDQnruZm6unQYfhis/Ife/bWfu0CHxnMc3Qam9VpEPk/vftp91gZ`+N5ilMOg2lbByAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAID//2Q=="];
They told about the dataWithBase64EncodedString: method which is not there in my version of xcode.
Please help.
You can make use of
UIImage *image = [[UIImage alloc] initWithData:data];
or
UIImage *image = [UIImage imageWithData:data];
The data in the data parameter must be formatted to match the file format of one of the system’s supported image types.
Try the following (where data is of the NSData type):
UIImage *img = [[UIImage alloc] initWithData: data];

Using an NSString and UIImageView to load image from URL, failing on some URLs

I've got some code to load an image from a URL, it seems to work ok. Unless the URL contains curly brackets. This seems like it is a string formatting problem? I can't figure it out.
ex
#"http://site/image.png //works
#"http://site/{image}.png //doesn't work
NSString* mapURL = #"http://site.com/directory/{map}.png";
NSLog(mapURL);
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageView setImage:image];
[imageData release];
[image release];
thanks
i found this helpful
http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/