I am trying to show list of images in a collection view inside App Notification Widget.
Its giving me the error "Unable to load" if I am loading from urls, other than its working fine.
Can anyone please help me in this ?
TodaysCusCell *catTrendingCusCell = (TodaysCusCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"TodaysCusCellID" forIndexPath:indexPath];
NSDictionary *trendingDic = (NSDictionary *)[trendingArr objectAtIndex:indexPath.row];
//Loading Images
NSString *imageURL = [trendingDic objectForKey:#"image_url"];
[catTrendingCusCell.trendingImgView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:#"grayBackground"]];
Related
This question already has answers here:
Getting Image from URL Objective C
(5 answers)
Closed 4 years ago.
I am trying to fetch images from URL and displaying it on collection view how to do it ?
I have used following code but didnt get answer
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
collectionGalleryTechrCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:Url];
//UIImage *image = [UIImage imageWithData:imageData];
//CollectionViewImage.image = image;
NSURL *urlBack = [NSURL URLWithString:#"http://ssk.com/zpschool1/web/gallery/download%20(4).jpg"];
NSData *urlDataBack = [NSData dataWithContentsOfURL:urlBack];
UIImage *imageBack = [UIImage imageWithData:urlDataBack];
cell.myImg.image =imageBack;
//cell.myImg.image = [UIImage imageWithData:ImgURL];
cell.myImg.userInteractionEnabled=YES;
return cell;
}
Loading and displaying image asynchronously is better then getting NSData from URL and displaying imageWithData in UICollectionView or UITableView.
You can check out this answer given in the link below :
https://stackoverflow.com/a/34041518/9332509
NSString *strImg_Path = [[NSString alloc]init];
strImg_Path = [NSString stringWithFormat:#"%#",[NSURL URLWithString:#"http://ssk.com/zpschool1/web/gallery/download%20(4).jpg"];
];
[cell.myImg sd_setImageWithURL:[NSURL URLWithString:strImg_Path]
placeholderImage:[UIImage imageNamed:#"placeholer_image"]];
make sure placeholer_image for default image and use framework - SDWebImage for lazy loading.
Use Url from your array for dynamic instead of static Url.
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"]]]];
I hava a uitableview , with custom cell containing two UImages. The logo images are taken from an online website, that's why there's a need to cache the images. Loading the image till now is made like this :
NSURL * imageURL = [NSURL URLWithString:[arra1 objectAtIndex:indexPath.row / 2]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSURL * imageURL2 = [NSURL URLWithString:[arra2 objectAtIndex:indexPath.row / 2]];
NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2];
cell.ima1.image = [UIImage imageWithData:imageData];
cell.ima2.image2 = [UIImage imageWithData:imageData2];
What i learned from searching , is that dataWithContentsOfURL is not asynchronous , and while scrolling it will take a lot of time. I tried several methods but i can't seem to get to right one. This is my first time caching UIImages , i would highly appreciate a detailed explanation with implementation so i could learn aside from getting the job done.
Many Thanks
I use this Library which is just perfect
SDWebImage
You just need to #import <SDWebImage/UIImageView+WebCache.h> to your project, and you can define also the placeholder when image is being downloaded with just this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// 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"]];
cell.textLabel.text = #"My Text";
return cell;
}
It also cache downloaded images and gives you great performance.
Hope it will help you!
SDWebImage, in my opinion, is the best option.
You simply include it in your app and use it like this:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:image_url]
options:0
progress:nil
completed:^(UIImage *images, NSError *error, SDImageCacheType cacheType, BOOL complete) {
myImageView.image = images;
}] ;
It download images asynchronously, so it does not block UI.
You can check these sample application
LazyTableImages - Sample application from Apple
MonoTouch-LazyTableImages
robertmryan- LazyTableImages - Explains clearly the limitations from apple's sample application.
Hope this helps.
Checkout UIImageLoader https://github.com/gngrwzrd/UIImageLoader
Easy to load an image, and you get callbacks for all the scenarios you would want to handle:
NSURL * imageURL = myURL;
[[UIImageLoader defaultLoader] loadImageWithURL:imageURL \
hasCache:^(UIImage *image, UIImageLoadSource loadedFromSource) {
//there was a cached image available. use that.
self.imageView.image = image;
} sendRequest:^(BOOL didHaveCachedImage) {
//a request is being made for the image.
if(!didHaveCachedImage) {
//there was not a cached image available, set a placeholder or do nothing.
self.loader.hidden = FALSE;
[self.loader startAnimating];
self.imageView.image = [UIImage imageNamed:#"placeholder"];
}
} requestCompleted:^(NSError *error, UIImage *image, UIImageLoadSource loadedFromSource) {
//network request finished.
[self.loader stopAnimating];
self.loader.hidden = TRUE;
if(loadedFromSource == UIImageLoadSourceNetworkToDisk) {
//the image was downloaded and saved to disk.
//since it was downloaded it has been updated since
//last cached version, or is brand new
self.imageView.image = image;
}
}];
Hi i have high resolution image in local(document). Now i want to list all local images in thumb nail size. I am using UICollectionView to show image while loading resize the image from high resolution to thumbnail. So scrolling speed lack in collectionView. Now i have confusion with how can i cache that resized thumbnail images. which one is the best option to do that. I have used SDWebimages to download and cached images from web. Thank you
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(#"%i path", indexPath.row);
CollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CollectionViewCell" forIndexPath:indexPath];
ImageData *imgData = [self readFromLocal:indexpath.item];
UIImage *thumb = [self resizeImgToTumb:imgData.image];
[cell.imgView setimage:thumg];
return cell;
}
I recommend storing the images on the file system in app-specific storage and reading those files when necessary. Use the NSFileManager class to accomplish this. Be sure you write code to clean up any files you create so that they don't take up too much space on the user's device.
UIImage *image = ...;
NSData *imageData = UIImagePNGRepresentation(image);
NSString *rootDirectory = [NSSearchPathForDirectoriesInDomain(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [rootDirectory stringByAppendingPathComponent:#"filename.png"];
NSError *error;
[imageData writeToFile:imagePath options:NSDataWritingAtomic error:&error];
If you are not completely worried about memory consumption you can create an NSDictionary and add your resized image to that. You will need to remove the objects from the dictionary once they are far enough off screen to be reloaded later.
[dictionary setObject:yourSmallImage forKey:indexPath];
Then retrieve it later:
UIImage *image = [dictionary objectForKey:indexPath];
if(!image)
{
//Load it from your source, no cache found
}
In my application I am parsing JSON data and then displaying that data in UITableView. The information is displayed in the table but the touch response is lagging really bad. I did some research and found out that it is recommended to implement asynchronous loading for the information and especially the image, but I could not find any relevant solutio that worked with my JSON application. I would appreciate some suggestions and comments of how to sort out this problem, here is the code:
jURL Definition www.website.com/info.json
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(jQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
jURL];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:NO];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* jsonDict = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
calRes = [jsonDict objectForKey:#"results"];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *calDict = [calRes objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString:[calDict objectForKey:#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.textLabel.text = [calDict objectForKey:#"name"];
cell.detailTextLabel.text = [calDict objectForKey:#"description"];
cell.imageView.image = imageLoad;
return cell;
}
I like to use the AFNetworking library for easy asynchronous image loading. You will need to include the library
#import "AFNetworking.h"
Then use it in the cellForRowAtIndexPath
[cell.imageView setImageWithURL:[NSURL URLWithString:[calDict objectForKey:#"image"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
You also need to supply a placeholder image. I use a blank JPG of the size you will want the final image will be.
You can use SDWebImage from this link : https://github.com/rs/SDWebImage
It is the simplest and fastest library for asynchronous image loading.
It also provides image caching.
You can just done the whole operations by simply calling this function:
[cell.imageView setImageWithURL:jURL
placeholderImage:[UIImage imageNamed:#"your place holder here"]];
Just enjoy it.
As a follow up, it will be good to cache the images locally using this very handy plugin by Jake Marsh: JMImageCache
This way, the images will not need to be loaded from the URL the next time you boot the app.
See, the lagging in cellforRowAtIndexPath is because of this
NSURL *imageURL = [NSURL URLWithString:[calDict objectForKey:#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
why don't you, use GCD for adding the image? Further more you can have your own NSCache to store the image so that every time the table reloads, the image can be directly loaded from the memory instead of firing the url.