I am getting URL images from API. I have stored the UR images into string. Here my problem I want to display that into carousel. I don't know how to do this. Below is my code that I have tried.
for(int i=0;i<hotelIMAges.count;i++ )
{
NSDictionary *images=[hotelIMAges objectAtIndex:i];
NSString *Images= [images objectForKey:#"Imagepath"];
[ImageARRAy addObject:Images];
} //Here I have stored all the images into string..(https://***.jpg)
Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0,300, 262)];
[carousel setImages:ImagesARRAy];
You just need to process asynchronously as your current approach can be a synchronous request and blocks the main thread until that download is complete.
Consider using SDWebImage and the UIImageView extension it adds to manage the image download and display. It also supports a placeholder image that you can supply to indicate that the download is in progress.
If your using any third party carousel check for asynchronous methods to display the images.I believe setImages: method must be for images that are stored locally inside app. Do let me know if you have any queries.
----EDITED----
Your Carousel is using synchronous method to display which blocks the main thread.
You can use SDWebImage for your purpose.
For this you need to download SDWebImage for asynchronous downloading of image. You can download SDWebImage from link below.
You can use it in your Carousel.h class as
#import <SDWebImage/UIImageView+WebCache.h>
modify your setup method with below line.
[imageView sd_setImageWithURL:[NSURL URLWithString:self.images[i]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
For details refer github page of SDwebpage. DO let me know if you have any queries.
https://github.com/rs/SDWebImage#using-asynchronous-image-downloader-independently
Related
I have a problem with loading of 5 images to each cell of TableView. I use AFNetworking for download and setImageWithUrl method for imageView:
for (int i = 0; i < urls.count; i++) {
[self.photoImageView[i] setImageWithURL:urls[i]];
}
TableView very brakes when I scroll it. But when i try to load 1 or 2 no brakes when scrolling.
How to correct download images without brakes?
I think's it's helpful for you below the code work for me.
First fall you use this library SDWebImage
then implement below code.
#import <SDWebImage/UIImageView+WebCache.h>
...
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"URL"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can use SDWebImage as discussed by #Ravi. You can use use the above code in your for loop to display all 5 images asynchronously , without blocking main thread.
for (int i = 0; i < urls.count; i++) {
[self.photoImageView[i] sd_setImageWithURL:[NSURL URLWithString:urls[i]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
If you dont want to use any third party libraries, you can also use GCD for the same. Check this ans.
Hope it helps. Happy Coding!!
If you see UIImage + AFNetworking class,It internally loads image using Async NSOperations.Try loading image with specific dimensions.It will download image of that size & could improve scrolling performance.
NSMutableString *newURLPath = [NSMutableString stringWithString[exercise.imageURL absoluteString]];
[newURLPath appendFormat:#"?width=200&"];
[newURLPath appendFormat:#"height=200"];
For downloading the Asynchronous image from url, use any of these library for improving the scroll performance
SDWebImage - Asynchronous image downloader with cache support as a UIImageView category.
#import <SDWebImage/UIImageView+WebCache.h>
...
[imageView sd_setImageWithURL:URL
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
];
PINRemoteImage - It download a progressive jpeg and get attractive blurred updates.
For in your case, you can go for SDWebImage.
For Advance Concept, you could look into this one.
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps
Even recently, Pinterest was built on top of AsyncDisplayKit to Keeps UIs smooth and responsive.
Read this article for more info: https://medium.com/#Pinterest_Engineering/re-architecting-pinterest-039-s-ios-app-e0a2d34a6ac2
So, I have this news feed structure. Every news block is a custom UITableViewCell. Now, every custom cell has a CollectionView that shows images.
The problem is that when scrolling, and news cell (news block) comes out visible, the CollectionView is reloading - every time cell shows up. I'm trying to find the way to cache those images on the main ViewController side.
What would be the best approach?
If you want to avoid implementing your own cache/disk/memory handlers for this kind of job I strongly recommend either AFNetworking or SDWebImage that handle it all for you.
An example of SDWebImage on how to set an image and its cache:
[imageView sd_setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
This handles all the cache/disk/memory for you automatically.
Here is example of how you handle cache with SDWebImage:
// Set memory size limit (check with older devices to avoid memory errors when setting it to high value.)
[[SDImageCache sharedImageCache] setMaxMemoryCost:****];
// Clear disk cache
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
}];
// Clear memory cache
[[SDImageCache sharedImageCache] clearMemory];
I would stay away from NSUserDefaults or the above mentioned example setting the images to dictionary or arrays to avoid memory errors and app crashes/performance.
You have to cache your UIImage instance, so, if you get some pictures from any source, you might write something like below:
/* some code before */
UIImage *yourImage = /* Get your UIImage from any available way */
[self.cache setObject:yourImage forKey:#"SomeUniqueID"];
/* some code after */
I guess, this code will look greater as a function, which obtains UIImage instance and it's id as parameters.
For my opinion, it's the most common way to store images. If you need a more proficient way without any overheads with third-party frameworks, you can read the documentation about NSCache.
But in general, you have a lot of different ways how to store your images:
Get the image from the disk
Get the image from the memory
Use one of the variants above, but apart them still use some lightweight & flexible frameworks, you can find a lot of on the GitHub.
So, the code I wrote above will just store images into the memory cache.
Iam using collectionview to load image.These images are loaded from url in background thread. Hence the scrolling is not smooth.The images in url get expired after seconds hence i want to update the image with new image.How can i use the SDWebImage to load image and make scrolling smooth? Can anyone please help me to solve this.
Use this code
[YOUR_CELL_NAME.YOUR_IMAGE_NAME setImageWithURL:[NSURL URLWithString:#"IMAGE URL"] placeholderImage:[UIImage imageNamed:#"PLACEHOLDER IMAGE NAME"]];
You need to integrate the sdwebimage classes in your code. After that make a import.
Download sdwebimage from here
#import "UIImageView+WebCache.h"
and in your collectionview method write following lines to download image in backend thread.
[YOUR_CELL_NAME.YOUR_IMAGE_NAME setImageWithURL:[NSURL URLWithString:#"IMAGE URL"] placeholderImage:[UIImage imageNamed:#"PLACEHOLDER IMAGE NAME"]];
I am loading images in my UITableViewCell from some web url and those images are in size around 80-100 kb per image. Everything works fine when i download images asynchronously but when i scroll the tableview, images start again downloading. Can i get rid of this as i have only limited no of rows.
I want if images from url download once should not be download again as it does makes view not comfortable for users when images starts downloading again.
I am using below code to load images in CellforRowAtIndexPath:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:url];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
cell.eventImage.image= [UIImage imageWithData:data];
});
});
You should use SDWebImage which will cache image on your device and if same image came from server then it load from your local device.
Add SDWebImage to your project.
import <SDWebImage/UIImageView+WebCache.h> header
then add below code at cellForItemAtIndexPath:
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"your image URL"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Better make use of SDWebImageCache for this because this a advantageous third party library to store the image in cache. After downloading once the images will be taken automatically from the cache.
SDWebImageCache
When you Download image put it to the document directory and before you download image check if document directory contains image then load it from there otherwise download from URL
If you want you can make it yourself. You can use NSCache in order to save your images. NSCache it is like NSMutableDictionary (save your image by key). And then you can check it. For key you can use path to your image.
But I also recommend you to use SDWebImage.
I'm using the SDWebImage plugin and I have run into the following scenario: I have two UIViewController's A and B. In UIViewControllerB I am loading a series of 10 - 15 images from the web in a UIImageView with the following code:
UIImageView *imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:[NSURL URLWithString:[sourceDictionary objectForKey:#"image"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
This works well. Except that I begin downloading these images once the user loads UIViewControllerB. I was wondering if there was a way from me to begin to pre-cache or begin downloading these images while the user is still on UIViewControllerA so that when the user gets to UIViewControllerB they see a shorter delay? How would I do this in a manner so that the image isnt downloaded twice if the user switches to the second viewcontroller while a download is taking place?
Possible workaround could be to implement custom download manager using Singleton pattern, so it's available from both A and B.
Then you can track the list of image URLs requested to be downloaded as well as completed handlers to be called when download completed.
First you check that the download of the image with same URL is not taking place already and then either add image into the list and completed handler or just add new handler. You can use dictionary as a storage, using URL as a key.
Then you can use SDWebImageManager with downloadImageWithURL method, where on completed method you remove image from the list and trigger corresponding completed handlers.
So at B you know that the download has been finished. You do not necessary need to cache images manually with SDWebImageCache, let SDWebImage to do it for you automatically, so when your on completed method get called just do the same as you do, but since image has been already downloaded SDWebImage will take it from the cache. Alternatively you can send back UIImage downloaded into the completed handler.