在xcode中将图像从一个视图传递到另一个视图?

时间:2012-11-30 06:53:35

标签: iphone xcode uiimageview uiimage

我需要将图像从一个视图传递到另一个视图。在第一个视图中,我从服务URL获取base64数据,并将其转换为特定图像并显示在tableviewcell中。现在,当我点击一个特定的单元格时图像应该从firstView传递到secondView.But无法得到它。出现错误"程序收到信号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];
}

我怎样才能得到它?

3 个答案:

答案 0 :(得分:0)

在课堂上制作一个功能

-(uiimage*)getImage{ return _urImage; }

或者 您可以使用 in class2

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

并可以通过首先创建其对象然后设置其属性来在2ndVC中设置图像 in 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。

答案 1 :(得分:0)

这一行错了:

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

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

您需要从UIImage获取UIImageView。你可以这样做:

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

或像这样的一行:

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

答案 2 :(得分:0)

首先尝试学习更好/标准的命名约定。 其次,accessoryButtonTappedForRowWithIndexPath中的UIImageView分配给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];
}