Asynchrone图像下载需要返回值

时间:2014-06-05 08:40:21

标签: ios objective-c asynchronous uiimageview sdwebimage

我试图在我的应用程序上设置asynchrone下载图像。 我按SDWebImage中的建议使用this issue

我在progresscompleted方法上设置了断点,一切正常。它工作得很好,但我的逻辑直接来自另一个问题。我不知道如何在UIImageView上异步设置我的图片。 一切都是动态的,每个图像都是独立调用的

以下是我的代码的一部分:

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

请注意CacheCache是我自己的缓存方法。

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;

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以试试这个,而不是寻找那个复杂的解决方案。创建名为“DownloadImagesAsynchronously”的NSObject文件,并使用以下替换.h文件。

#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

并使用以下代码替换.m

#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

现在在这样的UIImageView上调用,如果使用TableView,那么它将懒洋洋地将图像下载到每个tableview单元格

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

并实现委托方法。它会在下载图像时通知您。您可以执行所需的操作。

- (void)ImageDownloaded:(BOOL)_isDownloaded{

// Image Downloaded.

}

如果您有任何相关问题,希望这对您有用。请告诉我。感谢

答案 1 :(得分:0)

谢谢你们。

我把你的两个答案混在一起。我只是通过asynchrone块将UIImageView传递给我的方法。

这是我的代码:

//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;

         }
     }];
}

现在我只关于调整图片大小及其比例的问题,但原来的问题是固定的。

我希望这会帮助别人。