【问题标题】:Pass image from one view to another in xcode?在xcode中将图像从一个视图传递到另一个视图?
【发布时间】:2012-11-30 06:53:34
【问题描述】:

我需要将图像从一个视图传递到另一个视图。在第一个视图中,我从服务 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];
}

我怎样才能得到它?

【问题讨论】:

    标签: iphone xcode uiimageview uiimage


    【解决方案1】:

    这一行是错误的:

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

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

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

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

    或者像这样在一行中:

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

    【讨论】:

      【解决方案2】:

      在类中创建一个函数

      -(uiimage*)getImage{ return _urImage; }
      

      或者 您可以使用 2 类

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

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

      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。

      【讨论】:

        【解决方案3】:

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

        如下更正您的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];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-22
          • 1970-01-01
          • 2017-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          相关资源
          最近更新 更多