How to Integrate Latest Sdwebimage API in My Swift Based Project

Importing SDWebImage into Swift project

So nothing was working for me until I finally realized that you're supposed to use not the main SDWebImage folder, but the SDWebImage folder inside of that into the project. Dragging in the .xcodeproj file, the folder as a whole, or the .framework file didn't work. Importing that sub SDWebImage folder, and then utilizing Libor Zapletal's answer: #import "UIImageView+WebCache.h" is what finally got this thing working for me.

Lazy Loading : How to integrate SDWebImage with current project?

import this file in your .h

#import "UIImageView+WebCache.h"

and use following method,

[UIImageViewName setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];

How to install SDWebImage

When you add the SDWebImage folder in your project then select following option. To add copy of your folder to the destination project and Create groups.

and then you have to write just like

#import "UIImageView+WebCache.h"

And make sure that you are adding to all the targets that you want to use that library.

Sample Image

Documentation at Github :

Add the SDWebImage project to your project

  • Download and unzip the last version of the framework from the download page
  • Right-click on the project navigator and select "Add Files to "Your Project":
  • In the dialog, select SDWebImage.framework:
  • Check the "Copy items into destination group's folder (if needed)" checkbox

Or you can use cocoa pods as other answer suggested.

EDIT : EXAMPLE :

[self.imageView sd_setImageWithURL:[NSURL URLWithString:"yoururl.png"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

SDWebImage crash in swift

The SDWebImageModule is an actively developed open source component. It is much better in this case to raise a bug on GitHub. The developers have already made a few releases that fix Xcode6 and Swift issues.

Comprehending SDWebImage downloader

I've created a class for that, but here's how I use it:

- (void)setImageURL:(NSURL *)imageURL {
_imageURL = imageURL;
self.image = nil;
[self.layer removeAllAnimations];

if (!imageURL) {
self.image = self.placeHolderImage;
return;
}

__weak SDImageView *weakself = self;

self.progressView.hidden = NO;

SDWebImageDownloaderProgressBlock progressBlock =
^(NSInteger receivedSize, NSInteger expectedSize) {
if (expectedSize > 0) {
float progress = receivedSize / (float)expectedSize;
weakself.progressView.progress = MAX(MIN(1, progress), 0);
}
};

SDWebImageCompletionWithFinishedBlock successBlock =
^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (!image) image = self.placeHolderImage;

weakself.imageOperation = nil;
weakself.progressView.hidden = YES;
weakself.progressView.progress = 0.f;

if (cacheType == SDImageCacheTypeNone && !error) {
[UIView transitionWithView:self
duration:1.f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
weakself.image = image;
} completion:nil];
} else {
weakself.image = image;
}
};

SDWebImageManager *manager = [SDWebImageManager sharedManager];
self.imageOperation = [manager downloadImageWithURL:imageURL
options:SDWebImageRetryFailed
progress:progressBlock
completed:successBlock];
}


Related Topics



Leave a reply



Submit