我正在尝试在我的应用程序上设置asynchrone下载图像。 我正在使用本期中建议的SDWebImage 。
我把断点放在progress和completed方法上,一切正常。 它工作得很好,但我直接从我的逻辑中得到了另一个问题。 我不知道如何在我的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;谢谢您的帮助。
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.
最满意答案
您可以尝试这个,而不是寻求复杂的解决方案。 创建名为“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. }如果您有任何相关问题,希望这对您有用。 请告诉我。 谢谢
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.
更多推荐
发布评论