0

I'm trying to setup an asynchrone download of images on my application. I'm using SDWebImage as suggested in this issue.

I put breakpoints on the progress and completed method and everything is normal. It's working perfectly but I have another problem coming from my logic directly. I don't know how to set my image asynchronously on my UIImageView. Everything is dynamic and each image is called independently

Here is a part of my code:

 [myMenu->_userAvatar setImage:[[CacheCache sharedInstance] getUIImageFromPath:currentUser.avatarPhoto.avatarURL]]; 

Note that CacheCache is my own cache method.

NSURL* myURL=[NSURL URLWithString:path];
//NSData* myData=[NSData dataWithContentsOfURL:myURL];
NSData* myData;

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:myURL
         options:0
        progress:^(NSInteger receivedSize, NSInteger expectedSize)
 {
    DDLogInfo(@"Downloading...");
    // progression tracking code
 }
        completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
 {
    if (image && finished)
    {
       DDLogInfo(@"Downloaded !");
       image = [[UIImage alloc] initWithData:myData];
    }
 }];
...
return image;

Thank you for your help.

Community
  • 1
  • 1
brcebn
  • 1,571
  • 1
  • 23
  • 46
  • return image from completion block , the last return method will be called before completion block – Pawan Rai Jun 05 '14 at 08:43
  • I tried to return the image form the completion block but I have the following error `Control may reach end of a non-void block`. That seams completely normal because `downloadImageWithURL:options:progress:completed` can't return an `UIImageView` according to the documentation: `- (id )downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock` – brcebn Jun 05 '14 at 08:51
  • 1
    i searched for this , and found that downloadImageWithURL does not have a any return type(my fault). what i found is "downloadImageWithURL" method you can use for making your custom view that can download image & set to image . for example (UIImageView+WebCache.h). – Pawan Rai Jun 05 '14 at 09:19
  • for your case i think "setImageWithURL" method will do its work, let the SDWebCache do caching image work for you. – Pawan Rai Jun 05 '14 at 09:20

2 Answers2

1

Rather than going for that complex solution, you can try this one. Create NSObject file with name 'DownloadImagesAsynchronously' and replace .h file with following.

#import <Foundation/Foundation.h>

@protocol NotifyParentProtocol <NSObject>

-(void)ImageDownloaded:(BOOL)_isDownloaded;

@end

@interface DownloadImagesAsynchronously : NSObject{

    NSMutableData *receivedData;
    UIImageView *cCellImageView;
    NSURLConnection *imgDownloadConnection;

    id<NotifyParentProtocol> __weak delegate;
}

-(void) downloadImageAsynchornously:(NSString *)_imageURL andCellImage:(UIImageView *)_cellImgV;
@property(weak) id<NotifyParentProtocol> delegate;

@end

and replace .m with following code

#import "DownloadImagesAsynchronously.h"


@implementation DownloadImagesAsynchronously

@synthesize delegate;

- (void)loadWithURL:(NSURL *)url{

    NSURLConnection *conect = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url]delegate:self];
    [conect start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    cCellImageView.image = [UIImage imageWithData:receivedData];
    [cCellImageView.layer setCornerRadius:14.0f];
    [delegate ImageDownloaded:YES];
}

-(void) downloadImageAsynchornously:(NSString *)_imageURL andCellImage:(UIImageView *)_cellImage{

    cCellImageView = _cellImage;
    receivedData = [[NSMutableData alloc] init];
    NSString *baseURL = @"http://example.com/abc/Gallary";
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",baseURL,_imageURL]];
    [self loadWithURL:url];
}

@end

Now cal on your UIImageView like this, if using TableView then it will lazily download image to each tableview cell.

DownloadImagesAsynchronously *downloadAsynchImageObj = [[DownloadImagesAsynchronously alloc] init];
    downloadAsynchImageObj.delegate = self;
    [downloadAsynchImageObj downloadImageAsynchornously:model.ImageName1 andCellImage:self.mainImageView];

and implement delegate method. It will notify you when ever image is being downloaded. You can perform your desired action.

- (void)ImageDownloaded:(BOOL)_isDownloaded{

// Image Downloaded.

}

Hope this will work for you, if you have any question related this. Please let me know. Thanks

Shahab Qureshi
  • 952
  • 1
  • 7
  • 19
0

Thank you guys.

I mixed both of your answers. I simply passed my UIImageView to my method with asynchrone block.

Here is my code:

//view is an UIImageView coming directly from one of the parameters
if (view == nil) {
    myData=[NSData dataWithContentsOfURL:myURL];
    image = [[UIImage alloc] initWithData:myData];
}
else{
    [SDWebImageDownloader.sharedDownloader downloadImageWithURL:myURL
                                                        options:0
                                                       progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         DDLogInfo(@"Downloading...");
         // progression tracking code
     }
                                                      completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
     {
         if (image && finished)
         {
             DDLogInfo(@"Downloaded !");
             view.image = image;

         }
     }];
}

Now I only have a problem about the resize of my picture and its scale but my original issue is fixed.

I hope this will help someone else.

brcebn
  • 1,571
  • 1
  • 23
  • 46
  • 1
    hope this helps: https://gist.github.com/YGeorge/d8df24f29c349ec21a44 https://gist.github.com/YGeorge/8053506 – George Jun 05 '14 at 11:33
  • 1
    It helps me. It's almost fixed but I found inspiration coming from your example. Thank you ! – brcebn Jun 05 '14 at 13:02