我有一个表,它通过 XML 进行解析并获取由 URL 链接加载的图像的值。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
}];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
是的,它有效,但现在我需要修改我的 UIImage(带有类别,以使其大小合适),而我根本无法获得对它的引用。当我尝试像这样修改完成 block 中的图像时:
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
[image resizedImageWithBounds:CGSizeMake(66, 66)];
}];
绝对没有效果,即使我将图像更改为 nil,它实际上什么也没做,所以我的问题是:如何在方法中获取 UIImage 的实例变量:
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageFirst"]] placeholderImage:[UIImage imageNamed:@"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
}];
然后修改?或者我如何为我之前创建的这个方法传递 UIImage 的实例变量?这个问题听起来可能很愚蠢,但我需要建议。
任何帮助将不胜感激,谢谢!
最佳答案
假设没有错误返回到完成 block ,[image resizedImageWithBounds:CGSizeMake(66, 66)];
将为您创建一个新图像,但您仍然需要对其执行一些操作:
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
cell.imageView.image = image;
不过您确实需要小心,在图像准备好之前单元格可能已被重复使用,因此在 block 中使用 cell
可能是错误的。更好:
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = resizedImage;
关于ios - 无法使用 SDWebImage 框架获取对图像的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801181/