SDWebImage into NSArray - ios

How can I add 3 imageviews using SDWebImage into NSArray?
I have my images in this form:
[self.imageView1 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"image1"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[self.imageView2 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"image2"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[self.imageView3 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"image3"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

SDWebImage has another method with the completion block. Use that method
[self.imageView setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"image1"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
//completion code here ...
[self.animationArray addObject:image]; // animationArray is an array property
}];
Add the image to the animationArray from the completion block

If adding the 3 imageViews to your array is what you are aiming at then your second piece of code should be
NSArray *animationArray = [NSArray arrayWithObjects:self.imageView1, self.imageView2, self.imageView3, nil];
Although using the completion block is being suggested in the answer and maybe the best practice, this approach should also should manage fine considering that your imageWithURL call from SDWebImage will handle the network operation and update the objects when it receives the image.

Related

want to know if the image is not available at url using SDWebImage

in my application i am downloading an image from url from webservice using SDWebImage Library. I have nothing problem with this code
[imgQuestionContainer sd_setImageWithURL:[NSURL URLWithString:#"url"] placeholderImage:#"abc.png" options:SDWebImageProgressiveDownload];
with above code i have successfully load the image in imageview...
But my problem is that if i have url for the image but image is not available at that particular path of url. So, how to know that image is not available so i can load next data.
Thanks.
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(UIImage *image) {
if(image){
}else{
}
}
failure:^(NSError *error) {
}];
];
This is what exactly i wanted.
[imgQuestionContainer sd_setImageWithURL:[NSURL URLWithString:[[arrOfLoadedPuzzel objectAtIndex:0] objectForKey:#"puzzle_question"]]
placeholderImage:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
{
if(error)
{
// image is not available
}
else
{
// image is available
}
}];

SDWebImage animate images

I tried over 100 methods to animate images using SDWebImage but always fail. Do I understand good to using blocks? I am having IBOutlet property image names images1. I made array to store values and animate 3 images.
NSMutableArray *imagegallery = [[NSMutableArray alloc] init];
NSLog(#"IMAGES-%#", imagegallery);
[self.images1 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"Img1"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[imagegallery addObject:image];
NSLog(#"Ima1-------%#", image);
}];
[self.images1 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"Img2"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[imagegallery addObject:image];
}];
[self.images1 setImageWithURL:[NSURL URLWithString:[self.objc objectForKey:#"Img3"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[imagegallery addObject:image];
}];
self.images1.animationImages = imagegallery; //this line gets error
self.images1.animationDuration = 0.5;
[self.view addSubview:self.images1];
[self.images1 startAnimating];
I think when you set self.images1.animationImages = imagegallery because your image couldn't be loaded yet, method setImageWithURL:placeholderImage:completed: is an asynchronous function. you should try another way
like:
Animate images in uiimageview
The problem is that imageGallery contains strings as well as the images you add in the completed block. There's no reason to set imageGallery to be a copy of animatee in the first place since you never use those strings in imageGallery. Just create a new empty mutable array for imageGallery.

SDWebImage Image received size is NULL

I am trying to show progress view with SDWebImage but when i try below code then it return "0" in "receivedSize"
[cell. ImageHotDish setImageWithURL:[NSURL URLWithString:picName]
placeholderImage:[UIImage imageNamed:nil]
options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {
NSLog(#"Downloaded = %#",receivedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[cell.ImageHotDish setImageWithURL:[NSURL URLWithString:picName]];
}];
I am getting null velue in "receivedSize"
Can any one tell me where i am doing wrong
I have already check this solution but its not showing progress but just an activity indicator. What i am trying to get how much image have been downloaded.
Show activity indicator in SDWebImage

SDWebImage causing slow image binding

I am using SDWebImage to cache my images. Overall it looks good but when I use setImageWithUrl method, it take a bit longer to bind the image back to my collection view. Is there a solution to this problem? I tried the following, but looks like it's still having the same problem.
__weak UIImageView *weakImageView = pictureImageView;
[pictureImageView setImageWithURL:[NSURL URLWithString:picture.attachment.url] placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
weakImageView.image = image;
}];

iOS Collection View Scrolling Lags

I am using a UI Collection View to display cells with images and its associated data (like date, name) in a vertical scrolling format. The image data is fetched from the server when the view appears. The fetched data is then converted to UIImage when the cell appears.
The issue is scrolling is laggy. Everytime a new cell is about to appear, the scrolling hangs up a little. Is there a way to solve this problem? I was thinking of starting the conversion of NSData to UIImage in a background thread to prevent lag. Not sure if it is the right way.
As stack says, you should be loading the images asynchronously, and there are wonderful UIImageView categories that make this really easy to do, namely the category in SDWebImage or in AFNetworking. These UIImageView categories not only load images asynchronously, but also do caching, cancel network operations if necessary, etc. It takes a lot of code if you do this properly yourself (capturing all of those features I just enumerated), but it's really easy if you use one of these categories.
For example, if using SDWebImage, you can:
#import "UIImageView+WebCache.h"
and then:
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (error)
NSLog(#"%s: setImageWithURL error: %#", __FUNCTION__, error);
}];
Or, if using AFNetworking:
#import "UIImageView+AFNetworking.h"
and
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
or
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:url] placeholderImage:[UIImage imageNamed:#"placeholder.png"] success:nil failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"%s: setImageWithURLRequest error: %#", __FUNCTION__, error);
}];

Resources