how to fetch images from url on webView in ios - ios

http://www.imageurlhost.com/viewer.php?is_random=1&file=9psik7w1j5rkef8kek5.jpg
or
image code - 1wba395mv72m3of2ikz.jpg
i used this.
NSURL *imageURL = [NSURL URLWithString:#"http://www.imageurlhost.com/viewer.php?file=1wba395mv72m3of2ikz.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgview1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150,150 )];
imgview1.image= image;
[scrlview addSubview:imgview1];

you can set the image in image view like this
[imageView setImageWithURL:[NSURL URLWithString:#"your url string"] placeholderImage:[UIImage imageNamed:#"Placeholder.png"]];
placeholder.png is any temporary image which is replaced when image from url is received

Rahul paliwal ...Your url is html page..if you want load image in imageview use proper url...
if u want load image in imagview...like
this...

Related

Display Image fetched from the WebService

How to display an Image in the ImageView which is fetched from the WebService?
I am able to retrieve the Name of the image from the service successfully, but unable to set the image in the ImageView
NSString *imgName = [students objectForKey:#"Image"];
NSLog(#"%#", imgName); **/* Image1.png */**
imgImage.image = [UIImage imageNamed:imgName];
NSLog gives following output on Console :
Image1.png
I am unable to set the image in the ImageView
You can use the SDWebImage :
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
This is the link for download and how to use it:
https://github.com/rs/SDWebImage
You can also use this code set image
cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"your url"]]]];

Display pictures from URL into UICollectionView on iOS 7

How to display photos on URL into UICollectionView also make paging for photos , any help please
NSString *MyPatternString=[self.PatternImages objectAtIndex:indexPath.row];
cell.PattImagCollection.image=[UIImage imageNamed:MyPatternString ];
You can try this:
UIImage *image = [UIImage imageWithData:[NSData
dataWithContentsOfURL:[NSURL URLWithString:YourURL]]];
cell.yourimageviewname.image = image;

app slowing down due to image load issue

I am having problem with the loading of content in the app, I see that the data is fetched by the app but the images take lot of time to load, is there any possibility to load images afterwords. The code is below:
NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:#"photo"];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSString *u=[NSString stringWithFormat:#"http://%#",photoStrn];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *dpImage = [UIImage imageWithData:imageData];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
avatarimage.image = dpImage;
});
If you want more details I will provide :)
You can use GCD for doing this:
dispatch_async(dispatch_get_global_queue(0,0), ^{
for (NSDictionary *dic in discussionArray)
{
NSString *photoStr=[dic objectForKey:#"photo"];
NSString * photoString=[NSString stringWithFormat:#"http://%#",photoStr];
UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
}
});
Get SDWebImage Here and add that in your project and include
UIImageView+WebCache.h
in class implementation file
UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[jsonarray objectAtIndex:indexPath.row]valueForKey:#"imgurl"]]] placeholderImage:[UIImage imageNamed:#"Icon#2x.png"]];
[self.view addSubview:imag];
[imag release];
SDWebImage will be more useful for loading images from URL.
Hope this Helps !!!
James,
With this line,
[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]
you make a synchronous network call on the main thread. The current thread will hang untile the network call is complete.
The solution would be to do an asynchronous network call. The AFNetworking library provides a great category to load images asynchronously : UIImageView+AFNetworking.

"No visible #interface declares the selector" error

I inserted an image view in my view controller but dont actually know how to display image on it.
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
id path = #"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *firstImage = [[UIImage alloc] initWithData:data cache:NO];
}
But then it says that 'No visible #interface for UIimage declares the selector initwithData:cache'
I tried adding this line to the .h file but does not work either:
#property (nonatomic, retain) UIImageView *firstImage;
I know that the last step would be to assign that .h create class to the UIimage in the viewcontroller.
You can use try this code to add image from the path in your imageview
NSURL *url = [NSURL URLWithString:#"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
Don't forget to show your imageView as outlet to xib if you are adding through GUI otherwise add it to your view as subview
Try [[UIImage alloc] initWithData:data]; you can cache using an NSCachedURLResponse for the response.
You can use this to load images from URLs:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
But this will block your app until your image loads. It's best to load your images asynchronously when using images from the web. If the images are downloaded in a different thread user can keep interacting with the app. You can check this example from apple about lazy loading images: LazyTableImages
Or you can checkout this tutorial for async image download:
UItables with downloaded images

UIImage return Null IOS

NSURL *url = [NSURL URLWithString:
pictureUrl];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
NSLog(#"image width:%#",image.size.width);
I am getting null from this log? The image is valid and displaying correctly?.
This is in a table cell
Try this:
NSLog(#"image width:%f",image.size.width);
Besides that, I hope you are calling this on a background thread (not within cellForRow), otherwise loading your cells will be very slow and screw up user interaction.

Resources