ios - 无法使用 SDWebImage 框架获取对图像的引用

标签 ios objective-c uiimage sdwebimage

我有一个表,它通过 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/

相关文章:

ios - 如何在运行时正确压缩 UIImages

ios - Swift 2.x 无法设置 MapView 区域

ios - 如果添加了标签,则 presentModalViewController 无法显示

ios - 检测媒体库 ios 权限

iphone - 调整 UIImage 的内容

objective-c - UINavigationBar 后退按钮可拉伸(stretch)图像太长

ios - 如何从 ios9 连接到 postgresql

android - 将项目移植到另一个平台的方法

ios - iOS 6.1 中的闪烁屏幕问题

ios - 如何创建带边框的圆形图像(UIGraphics)?