【问题标题】:show an image as enlarged on clicking an image view in iPhone在 iPhone 中单击图像视图时显示放大的图像
【发布时间】:2012-07-08 18:30:57
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: iphone objective-c xcode uitableview uiimageview


    【解决方案1】:

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

    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。

    【讨论】:

    • 点击图像视图并没有调用图像点击方法。可能是原因吗?
    • @alpz 我修改了我的答案,使用户交互启用到 imageview。
    • 但较大的图像始终显示在同一位置。我需要在表格视图本身的特定单元格上显示放大的图像。因此还需要单元格的标识符。是吗?
    • 我在 cellforrow 创建时为图像添加了标签,并在点击功能中使用。
    【解决方案2】:

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

    【讨论】:

    • 我像这样添加了 btn。但它没有调用更改大小方法
    • 确保 cell.imageView.userInteractionEnabled = YES
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

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

      你需要为 cell.button1 提供背景图片 它可能对你有帮助。,。

      我正在学习。,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多