单击图像视图弹出显示原始图像的视图

时间:2013-04-18 06:24:44

标签: iphone ios uiimageview tableview afnetworking

我已将多张图片加载到表格视图中。图像已经调整大小,我希望当用户点击图像视图时,它会弹出一个显示原始图像大小的视图,当用户再次点击任何地方时,视图会消失并返回到桌面视图。加载图像的代码如下:

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    SearchCell *cell = (SearchCell *)[atableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:self options:nil] objectAtIndex:0];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.name.text = [[self.brands objectAtIndex:indexPath.row] objectForKey:@"name"];

    __weak SearchCell *weakCell = cell; 
    [cell.leftImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s-57130.gotocdn.com/%@", [[self.brands objectAtIndex:indexPath.row] objectForKey:@"pic"]]]] placeholderImage:[UIImage imageNamed:@"app_icon.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

        CGFloat imageHeight = image.size.height;
        CGFloat imageWidth = image.size.width;

        CGSize targetSize = CGSizeMake(70, 70);
        CGSize newSize = targetSize;
        CGFloat scaleFactor = targetSize.width / imageWidth;
        newSize.height = imageHeight * scaleFactor;

        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *small = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        weakCell.imageView.image = small;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    }];
    return cell;
}

1 个答案:

答案 0 :(得分:0)

无需手动调整图像大小。如果您将imageView contentMode属性设置为UIViewContentModeScaleAspectFill,它会自动缩放以适合图片视图的大小。

对于点击缩放,请查看UIImageViewModeScaleAspect