gpt4 book ai didi

ios - AFNetworking 2.0 和下载多张图片

转载 作者:行者123 更新时间:2023-11-29 10:47:34 25 4
gpt4 key购买 nike

我认为我的问题出在我的代码某处或逻辑上。

我想做什么

我在 view Controller 中有一个 UICollectionView在 Collection View 中,我有一个自定义的 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 的 setter 。每个项目仅开始下载 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 的图像都设置为相同的图像。当我重新加载 View 或关闭应用程序并再次打开它时,这总是一个不同的图像,它将是一个不同的图像。我认为这是下载和设置的最后一张图片。

为什么我认为它正在发生

我认为原因是因为每次提出新的请求我都会取消之前的请求?任何人都可以阐明这个问题吗?

最佳答案

如果你想创建一个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

调用它

希望对您有所帮助。

关于ios - AFNetworking 2.0 和下载多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21900967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com