簡體   English   中英

AFNetworking 2.0並下載多張圖像

[英]AFNetworking 2.0 and downloading multiple images

我認為我的問題出在我的代碼或邏輯中。

我要做什么

我在view Controller有一個UICollectionView在集合視圖中,我有一個自定義UICollectionViewCell其自定義原因是需要為每個單元格下載並設置不同的圖像。 這些圖像中的每一個都是具有其他屬性(例如日期/標題等)的項目。

這就是我嘗試過的

創建了一個自定義UITableViewCell ,在該類中,我有以下代碼:

 -(void) setDetailsWithTitle: (NSString *) title Image:(UIImage *) image Items: (NSArray *)items
    {
        int i = 0;

        for (BBItem *item in items){

            BBThumbnailView *thumbNailView = [[BBThumbnailView alloc]initWithFrame:CGRectMake(5 + (60 * 1), 5, 60, 60)];
            [self.contentView addSubview:thumbNailView];
            thumbNailView.item = item;
            thumbNailView.clipsToBounds = YES;

            i++;
        }
    }

在這里,我給單元格一個包含所有項目的數組。 這些項目是對象。 我下載了它們,並使用XML解析器對其進行了解析。 完美地工作,並且在檢查每個對象時-它們都具有我需要的正確屬性。

我還沒有在此方法中使用UIImage參數。 for循環遍歷數組中的每個項目並設置圖像,並且thumbNailView.item是我設置的對象。

然后在BBThumbnailView我這樣做:

設置每個項目並獲取其URL

 (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {


        self.shouldShowPlaceHolder = NO;
        self.backgroundColor = [UIColor clearColor];

        UIImageView *backGroundImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Thumbnail_Background_Big.png"]];


        [self addSubview:backGroundImageView];

        self.layer.masksToBounds = NO;

        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 2, 60, 60)];

            //  self.imageView.backgroundColor = [UIColor whiteColor];

        [self addSubview:self.imageView];
    }
    return self;
}

self.imageView is the imageView I am setting for the cells. The other imageViews are placeholder images. 

-(void)setItem:(BBItem *)aItem
{

    _item = aItem;
    if (self.item){

        if (self.item.thumbnailUrl && [self.item.thumbnailUrl length] > 0){

            [self loadUrl: [NSURL URLWithString:self.item.thumbnailUrl]];
        }
    }

}

這是我覆蓋屬性BBItem的設置方法。 每個項目僅開始下載URL。

loadUrl方法中:

-(void)loadUrl:(NSURL *)url
{
    NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url]; 
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            //NSLog(@"Response: %@", responseObject);
            //self.imageView.image = responseObject;
        UIImage *image = [[UIImage alloc] init];
        image = responseObject; 
        [self.imageView setImage:image];



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image error: %@", error);
    }];
    [requestOperation start];

}

現在,我在lodUrl方法上設置了一個斷點,並執行了很多次(有很多BBItems),每個項目都有不同的URL。

問題

每個BBCollectionViewCell的圖像都設置為同一圖像。 當我重新加載視圖或關閉應用程序並再次打開它時,這始終是不同的圖像,它將是不同的圖像。 我認為這是下載並設置的最后一張圖片。

為什么我認為這正在發生

我認為原因是由於提出的每個新請求都導致我取消了先前的請求? 誰能闡明這個問題?

如果要創建AFHTTPRequestOperation隊列, AFHTTPRequestOperation可以使用: NSOperationQueue *queue 但我不認為這是問題。.我認為您應該這樣做(我更改了功能):

-(void) setDetailsWithTitle: (NSString *) title Image:(UIImage *) image Item: (BBItem *)item
{
    BBThumbnailView *thumbNailView = [[BBThumbnailView alloc]initWithFrame:CGRectMake(5 + (60 * 1), 5, 60, 60)];
    [self.contentView addSubview:thumbNailView];
    thumbNailView.item = item;
    thumbNailView.clipsToBounds = YES;
}

您應該為每個cell格調用它

希望對您有所幫助。

我要說的是您不是將操作放在操作隊列中。 因此,當您在操作上調用start時,它會立即在后台線程上執行,這可能一次只允許執行一次單個操作。

2個問題。 為什么不使用UIImageView + AFNetworking?

為什么不批量提交圖像請求? 或者至少在NSOperationQueue上運行它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM