在iPhone中单击图像视图时显示放大的图像

时间:2012-07-09 10:57:28

标签: iphone objective-c xcode uitableview uiimageview

我已经实现了异步图像视图以在我的自定义单元格视图上加载图像,但图像在单元格上显示为小。因此,当点击图像视图时,我需要显示该图像的放大形式。我怎样才能做到这一点?

这是我的代码:

 if (cell == nil) {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     //create new cell
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

     //common settings
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
     cell.layer.cornerRadius = 10; 
     cell.layer.masksToBounds = YES;

     cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
     cell.imageView.frame = CGRectMake(10.0f, 10.0f, 60.0f, 44.0f);
     cell.imageView.backgroundColor = [UIColor colorWithRed:73 green:73 blue:73 alpha:1];

     cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
     imgBtn=[cell imageView];
     objectAtIndex:indexPath.row]objectForKey:@"Name"];
     cell.imageView.imageURL =  [NSURL URLWithString:[[detailsList objectAtIndex:indexPath.row]objectForKey:@"image_url"]];
}

我想在触摸单元格内的图像视图时放大图像。有任何想法吗?提前谢谢。

4 个答案:

答案 0 :(得分:3)

您可以使用手势识别器添加图像,如下所示: -

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];           
tapped.numberOfTapsRequired = 1;
[image setUserInteractionEnabled:YES];
image.tag = indexPath.row;
[image addGestureRecognizer:tapped];   
[cell.contentView addSubview:image];

你的imagetapped函数将是: -

 - (void)imageTapped:(UITapGestureRecognizer *)gesture 
{
    UIImageView *img = (UIImageView *)[gesture view];
    BigimgView.image = img.image
    [self.view addSubview:BigimgView];
    UITableViewCell *cell = [MyTableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:img.tag]];

}

和BigimgView可以是大尺寸的imageview的iboutlet。

答案 1 :(得分:1)

当您点按imageView时,您想收到一条消息? 一种可能的解决方案是创建自定义UITableViewCell,可能在自己的xib文件中。

然后你的问题是让你的自定义UITableViewCell上的UIImageView可以点击。 请看一下这个问题:here

更简单的解决方案是使用UIButton,而不是UIImageView。您可以将图像/ backgroundImage设置为UIButton。

答案 2 :(得分:1)

尝试使用UIButton:

UIButton * b = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f , cell.imageView.frame.size.width, cell.imageView.frame.size.height)];
b.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[b addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventTouchUpInside];
[cell.imageView addSubview:b];

-(IBAction)changeSize:(UIButton *)sender
{
    UIImageView * imageView = (UIImageView *)[sender superview];
    imageView.frame = //Your larger frame goes here
}

答案 3 :(得分:1)

在您的自定义单元格中添加按钮。

@interface custom : UITableViewCell
{
    UIButton *button1
}

然后在xib中添加按钮。然后连接到该按钮。

来到tableview数据源方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    /// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    custom *cell=(custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSArray *topView=[[NSBundle mainBundle]loadNibNamed:@"custom" owner:self options:nil];

        for(id currentobject in topView)
        {
            if ([currentobject isKindOfClass:[UITableViewCell class]])
            {
                cell=(custom *)currentobject;
                //cell.backgroundView.backgroundColor=[UIColor clearColor];

                 break;
             }
         }
    }   
    //  0.0f, 0.0f , cell.imageView.frame.size.width,        cell.imageView.frame.size.height
    [cell.button1 addTarget:self action:@selector(changeSize) forControlEvents:UIControlEventTouchUpInside];
}

现在点击button.it将转到该方法。,。,试试。,。,

-(IBAction)changeSize:(UIButton *)sender
{
    UIImageView * imageView = (UIImageView *)[sender superview];
    imageView.frame = //Your larger frame goes here
}

你需要给出cell.button1的背景图片 它可以帮助你。,

我正在学习。,