【问题标题】:Resize downloaded image from URL using SDWebImage from YouTube JSON EDITED使用 YouTube JSON 中的 SDWebImage 调整从 URL 下载的图像的大小 已编辑
【发布时间】:2012-05-30 16:59:59
【问题描述】:

我正在使用 SDWebImage 从 youtube JSON Web 服务下载缩略图,以便将它们设置在表格视图单元格中。问题是下载图像是 120x90,对我的需要来说太大了,因为我想把它放在 75x75 的 UIImageView 中。虽然图像缓存在占位符中,但它们看起来是正确的,但是一旦缓存完成,它们就会以 120x90 的比例大小显示。我试过setContentMode:UIViewContentModeScaleToFill,但还是不行,所以不知道怎么解决。

NSString *path= [thumbnail valueForKey:@"sqDefault"];

        [cell.imageView setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

        [cell.imageView setContentMode:UIViewContentModeScaleToFill];

        CALayer * l = [cell.imageView layer];

        [l setMasksToBounds:YES];

        [l setCornerRadius:5.0];

非常感谢

已编辑:

我在自定义单元格扩展类中设置了内容模式:

newsCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.Image   setContentMode:UIViewContentModeScaleToFill];

    }


    return self;
}

这里是单元格创建代码:

多媒体.m

 newsCell *cell = [self.tableView 
                          dequeueReusableCellWithIdentifier:@"news"];

        if (cell == nil) {
            cell = [[newsCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"news"];
        }

问题是 if (cell == nil) 永远不会被调用,因为第一个语句正在创建单元格,因此不会调用 initWithStyle 并且未设置内容模式,但是如果我删除第一个语句,则不会创建单元格。我不知道如何管理它。

谢谢

【问题讨论】:

    标签: objective-c ios5


    【解决方案1】:

    试试这两个选项,看看哪个更适合您的需求:

    UIViewContentModeScaleAspectFit
    UIViewContentModeAspectFill
    

    如果这些不起作用,我猜这是因为 UITableViewCell 正在 UIImageView 上设置 contentMode - 在这种情况下,您应该创建一个 UITableViewCell 的子类,其中包含一个 UIImageView 并设置了正确的 contentMode。然后你可以在你的 UITableView 中使用你自定义的 UITableViewCell 子类。

    【讨论】:

    • 它们不起作用,但 Xcode 声称使用 UIViewContentModeScaleAspectFit 而不是 UIViewContentModeAspectFit。谢谢
    • 我添加了一些可能对您有所帮助的信息。
    • 迈克尔,我按照你的指示,我已经编辑了问题,所以我在初始化自定义单元格时遇到了问题。非常感谢
    【解决方案2】:

    在其自定义单元格类中设置表格图像视图的自定义大小

        - (void)layoutSubviews {  
        [super layoutSubviews];  
        self.imageView.frame = CGRectMake(5,5,40,32.5);  
        float limgW =  self.imageView.image.size.width;  
        if(limgW > 0) {  
            self.textLabel.frame = CGRectMake(55,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);  
            self.detailTextLabel.frame = CGRectMake(55,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width,self.detailTextLabel.frame.size.height);  
        }  
    }  
    

    更多详情please refer this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多