如何使用segue将图像从表格单元格传递到UIView中的UIImage对象?

时间:2012-12-18 08:58:50

标签: iphone objective-c ios uiimage segue

我有一个表视图,UIView.Table视图单元格中有带image的单元格。我想在选择perticuler单元格时,相应的图像应出现在UIView中的“UIImage对象”中。

在表格视图中:

//I'm getting temp image as following(in my "cellForRowAtIndexPath" method)
  if(indexPath.row == 0)
    {
        [[cell imageView]setImage:[UIImage imageNamed:@"greekChickenSalad.png"]];
        tempImage = [UIImage imageNamed:@"greekChickenSalad.png"];
    }

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SubRecipeDescriptionSegue"])
{
     RecipeDescriptionViewController *rdVC;
    rdVC = segue.destinationViewController;
    rdVC.parentIndexRDVC = descriptionIndexPath;
    rdVC.DescriptionLabel = (UILabel*)subRecipeSelected;
    rdVC.image = tempImage;

}

}

在UIView中,LoadView方法添加如下图像:

NSData *imageDataString = UIImagePNGRepresentation(image);
NSString *imageNamedContent = [[NSString alloc]initWithBytes:[imageDataString bytes] length:[imageDataString length] encoding:NSASCIIStringEncoding];
UIImage *image1 = [UIImage imageNamed:imageNamedContent];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image1];
imageView.frame = CGRectMake(30,30, 250, 100);
[self.view addSubview:imageView];

在上面的代码中,我将图像转换为NSString([UIImage imageNamed:imageNamedContent]) - 这是正确的吗?

如果没有,我该怎么做?

注意:如果我将表格单元格的文本传递给UIView中的标签,Segue工作正常。

希望我的问题很明确。

1 个答案:

答案 0 :(得分:0)

变化:

NSData *imageDataString = UIImagePNGRepresentation(image);
NSString *imageNamedContent = [[NSString alloc]initWithBytes:[imageDataString bytes] length:[imageDataString length] encoding:NSASCIIStringEncoding];
UIImage *image1 = [UIImage imageNamed:imageNamedContent];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image1];

分为:

UIImageView *imageView = [[UIImageView alloc]initWithImage:image];


您还需要获取当前单元格的图像并使用performSegueWithIdentifier发送它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"SubRecipeDescriptionSegue" sender:cell.imageView.image];
}

然后您需要更新prepareForSegue方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SubRecipeDescriptionSegue"])
    {
        UIImage *selectedImage = (UIImage*)sender;

        RecipeDescriptionViewController *rdVC;
        rdVC = segue.destinationViewController;
        rdVC.parentIndexRDVC = descriptionIndexPath;
        rdVC.DescriptionLabel = (UILabel*)subRecipeSelected;
        rdVC.image = selectedImage;
    }
}