How to Use SDWebImageManager in CCLayer
1. In the header file
# Import "SDWebImageManager. h"
CCLayer must be implemented in the class definition
SDWebImageManagerDelegate.
Define and create
UIImageView * m_headImage;
2. Start downloading Avatar
- (void) upDateUserAvatar:(std::string) strAvatar{ if (strAvatar.length()<=0) { return ; } NSString * nsStrAvatar=[NSString stringWithCString:strAvatar.c_str() encoding:NSUTF8StringEncoding]; if ([nsStrAvatar length] != 0) { SDWebImageManager* wim = [SDWebImageManager sharedManager]; [wim cancelForDelegate:self]; NSURL* url = [NSURL URLWithString:nsStrAvatar]; UIImage* cachedImage = [wim imageWithURL:url]; if (cachedImage) { m_headImage.image=cachedImage; } else { [wim downloadWithURL:url delegate:self]; }}}
3. Pair
SDWebImageManagerDelegate implementation code
//SDWebImageManagerDelegate delegate implement- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image{ UNUSED(imageManager); m_headImage.image=image;}- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error{ UNUSED(imageManager); UNUSED(error);}
4. Cancel download in OnExit
-(Void) onExit {// cancel the download request submitted by self [[SDWebImageManager sharedManager] cancelForDelegate: self]; [super onExit];}