Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使用segue将图像从表格单元格传递到UIView中的UIImage对象?_Iphone_Objective C_Ios_Uiimage_Segue - Fatal编程技术网

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

Iphone 如何使用segue将图像从表格单元格传递到UIView中的UIImage对象?,iphone,objective-c,ios,uiimage,segue,Iphone,Objective C,Ios,Uiimage,Segue,我有一个表视图和UIView。表视图单元格有带图像的单元格。我希望当选择了Perticular单元格时,相应的图像应该出现在UIView的“UIImage对象”中 在表视图类中: //I'm getting temp image as following(in my "cellForRowAtIndexPath" method) if(indexPath.row == 0) { [[cell imageView]setImage:[UIImage imageNamed

我有一个表视图和UIView。表视图单元格有带图像的单元格。我希望当选择了Perticular单元格时,相应的图像应该出现在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 ImageName:imageNamedContent])-这是否正确

如果没有,我怎么做

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

希望我的问题清楚

变化:

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];
进入:



此外,您还需要获取当前单元格的图像,并将其与PerformsgueWithIdentifier一起发送:

- (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;
    }
}

您没有将图像转换为字符串。您正在使用作为字符串的文件名创建新图像。这是图像到字符串的转换…NSData*imageDataString=UIImagePNGRepresentation(图像);NSString*imageNamedContent=[[NSString alloc]initWithBytes:[imageDataString bytes]长度:[imageDataString length]编码:NSASCIIStringEncoding];我的“映像是一个属性”。是的,我只是这样做。我在“RecipeDescriptionViewController”中有如下属性:@property(非原子,保留)ibuiImage*映像;而且,在Segue sender(上面的代码)中,我正在做“rdVC.image=tempImage;”谢谢你的回答。在这种情况下,无论我选择哪个单元格,我都会得到最后一个单元格的图像。更新我的答案,如果你需要更多帮助,请告诉我。谢谢。我用错误的方法选择图像。现在我将其更改为“didSelectRowAtIndexPath:”并且工作正常。
-(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;
    }
}