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



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

这是我的代码:

 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"]];
}

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

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

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];

您的图像点击功能将是:-

 - (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可以是大尺寸图像视图的iboutlet。

尝试使用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
}

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

那么您的问题将是使自定义UITableViewCell上的UIImageView可单击。请看一下这个问题:这里是

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

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

@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];
}

现在单击按钮。它将转到该方法。,。,试试看

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

你需要给出单元格的背景图像。按钮1它可能会帮助你。

我在学习。,

最新更新