Display Image fetched from the WebService - ios

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

Related

How to load images using SDWebImage library

Hi I am using SDWebImage for loading images ok that's fine
Here I am getting images from services and I want to resize those images and I want to load them using SDWebImage
for this I wrote the code below
Obj.imageYH---> this is imageURL
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:Obj.imageYH]];
UIImage *actualImage = [UIImage imageWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(150, 150));
[actualImage drawInRect:CGRectMake(0,0,150, 150)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *smallData = UIImagePNGRepresentation(newImage);
NSString *Finalstring = [[NSString alloc] initWithData:smallData encoding:NSUTF8StringEncoding] ;
[Mainimage sd_setImageWithURL:[NSURL URLWithString: Finalstring] placeholderImage:[UIImage imageNamed:#"collectionViewIcon.png"]];
but in the code above, I am getting null value in Finalstring. How can I resize it and how can I load images using SDWebImage?
Step 1: Installation of SDWebImage
To start with, make sure you're using the latest version of SDWebImage, and installed via CocoaPods (not recommend to link up the library manually).
Your Podfile should contain this line:
pod 'SDWebImage', '~>3.7'
After that, close the Xcode project and run pod install in your project directory. Use the Xcode workspace after installation.
Step 2: Using the codes
First of all, you have the import the library
#import <SDWebImage/UIImageView+WebCache.h>
At this moment, try to Clean & Build the Workspace. If everything goes well, the project should be built without error.
Then, in your function:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:Obj.imageYH
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code, optional.
// You can set the progress block to nil
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// Resize your image here, then set it to UIImageView
Mainimage.image = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:0.5f];
// resize to 0.5x, function available since iOS 6
}
}];
done.
Use below line of codes instead of your code :
UIImage *actualImage = [UIImage imageWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(150, 150));
[actualImage drawInRect:CGRectMake(0,0,150, 150)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *smallData = UIImagePNGRepresentation(newImage);
[smallData writeToFile:Finalstring atomically:YES];// Finalstring is file path of resize image
[Mainimage sd_setImageWithURL:[NSURL URLWithString: Finalstring] placeholderImage:[UIImage imageNamed:#"collectionViewIcon.png"]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:url delegate:self options:0 success:^(UIImage
*image) {
cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(cell.profilePicture.frame.size.width,
cell.profilePicture.frame.size.height) image:image]; } failure:nil];
Don't forget to include
#import <SDWebImage/UIImageView+WebCache.h>
#interface YourViewController : UIViewController <SDWebImageManagerDelegate>

How to load an image from URL in app Extension?

I have implemented an app extension for my application, but i am facing an issue when trying to load an image from a URL into an imageView.
I tried to use PAImageView and UIImageView but both with failure.
The code that i was using for PAImageView is the following:
[self.imageView setImageURL:[NSURL URLWithString:#"https://blabblaLogo.jpg"]];
self.userImageView.clipsToBounds = YES;
and tried to use SDWebImage for UIImageView with the following:
[self.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:#"default.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
and the image doesnt appear in both cases. Note that a default image from assets is displayed correctly without any issue.
Is it possible to load an image from a URL in an app Extension? and how can we achieve that?
Thank you.
Am using something like this and it's work good until this issues fixed
cell.avatar.image = [UIImage imageNamed:#"selection-box_emty.png"];
// Load the image with an GCD block executed in another thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:Arr[indexPath.row][#"avatar"]]];
if (data) {
UIImage *offersImage = [UIImage imageWithData:data];
if (offersImage) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *offersImage = [UIImage imageWithData:data];
cell.avatar.image = offersImage;
});
}
}
});
I faced a similar problem, put breakpoints into into library and problem apparently was App Tranport Security. We need separate App Transport Security Settings for every extension we add
Try This in your Image url
If image url contain any space it will add %20 and work fine
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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;

UIImage not loading URL

My UIImage is not loading the URL I specified with [NSData datawithContentsOfURL:], even though the URL is (seemingly) correct.
Here's my configureCell: method:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSString *url = [photoUrls objectAtIndex:indexPath.section];
cell.imageView.image = [UIImage imagewithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
}
Here's what is inside of the "photoUrls" array (excluding bullet points):
(
http://fbcdn.net/fakeurl1/839.jpg
http://fbcdn.net/fakeurl2/840.jpg
http://fbcdn.net/fakeurl3/841.jpg
)
I wouldn't use [nsdata dataWithContentsOfURL:] in configure cell as it will block the UI. And also the way you have it, it will redownload all the images over and over again.
The following links will help you download the data asynchronously with out blocking the UI.
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial
https://github.com/rs/SDWebImage
With This SDWebImage all you have to do is:
[cell.imageView setImageWithURL:[NSURL URLWithString:urlString]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
it will take care of downloading the image.
Your URLs don't point to valid images. All I see is "Not found. Back to facebook". Your call to [NSData dataWithContentsOfURL:] will get the data, but [UIImage imageWithData:] will fail to generate a UIImage because the data doesn't represent a valid image.

how to fetch images from url on webView in 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...

Resources