0

我需要将图像从一个视图传递到另一个视图。在第一个视图中,我从服务 URL 获取 base64 数据,并将其转换为特定图像并显示在 tableviewcell 中。现在,当我点击特定单元格时,相应的图像应该是从 firstView 传递到 secondView。但无法获取它。出现错误“程序收到信号 SIGABRT”

这是我正在处理的代码:

FirstView.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
          NSData *data = [NSData dataFromBase64String:yBase64String];        
          UIImage* image = [UIImage imageWithData: data];

          UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,10,90,150)];              
          myImageView.tag = 120;
          myImageView.image = image;
          [cell addSubview:myImageView];
          [myImageView release];
          return cell;    

}
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
  SecondView *scnd = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
  UIImage *image=(UIImage *)[cell viewWithTag:120];
  scnd.provImage=pImage;
  [self presentModalViewController: scnd animated:NO];
}

SecondView.m
-(void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *myImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(2,25,90,80)]; //getting error at this line 
    myImageView.image= self.provImage;
    [self.view addSubview: myImageView];
    [ myImageView release];
}

我怎么才能得到它 ?

4

3 回答 3

0

在课堂上做一个函数

-(uiimage*)getImage{ return _urImage; }

或者你可以 在 class2中使用

 @property (nonatomic,retain) myImg;
    @synthesis myImg 

并且可以通过首先在 2ndVC 中创建其对象然后 在 class1 中设置其属性来设置图像

NSData *data = [NSData dataFromBase64String:yBase64String];        
          _image = [UIImage imageWithData: data];
2ndVCObj.myImg = _image;
//call [2ndVC viewDidload]; again

就像在你的代码中一样 -

FirstView.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
          NSData *data = [NSData dataFromBase64String:yBase64String];        
          UIImage* image = [UIImage imageWithData: data];

          UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,10,90,150)];              
          myImageView.tag = 120;
          myImageView.image = _image;
          [cell addSubview:myImageView];
          [myImageView release];
          return cell;    

}
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
  SecondView *scnd = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
  UIImage *_image=(UIImage *)[cell viewWithTag:120];
  scnd.provImage= _image;
// check image should not be null
// insert here
[scnd viewDidLoad];
//call [2ndVC viewDidload]; again
  [self presentModalViewController: scnd animated:NO];
}

SecondView.m
-(void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *myImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(2,25,90,80)]; //getting error at this line 
    myImageView.image= self.provImage;
    [self.view addSubview: myImageView];
    [ myImageView release];
}

它会起作用的。另请参阅您可以为全局图像创建一个数组并相应地设置 img。

于 2012-11-30T07:01:19.837 回答
0

这一行是错误的:

UIImage *image=(UIImage *)[cell viewWithTag:120];

viewWithTag:将返回一个视图,在本例中为 a UIImageView,您将其视为 a UIImage

您需要UIImageUIImageView. 你可以这样做:

UIImageView *imageView = (UIImageView *)[cell viewWithTag:120];
UIImage *image = imageView.image;

或者像这样的一行:

UIImage *image = [(UIImageView *)[cell viewWithTag:120] image];
于 2012-11-30T07:08:15.317 回答
0

首先尝试学习更好/标准的命名约定。accessoryButtonTappedForRowWithIndexPath其次,当您将 a 分配给 aUIImageView时,崩溃很明显UIImage

更正您accessoryButtonTappedForRowWithIndexPath的如下

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
  SecondView *scnd = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
  UIImageView *imageView=(UIImageView *)[cell viewWithTag:120];
  scnd.provImage=imageView.image;

  [self presentModalViewController: scnd animated:NO];
}
于 2012-11-30T07:11:01.527 回答