0

あるビューから別のビューに画像を渡す必要があります。firstView では、サービス URL から base64 データを取得し、それを特定の画像に変換して tableviewcell に表示しました。特定のセルをタップすると、対応する画像が表示されます。 firstView から secondView に渡されましたが、取得できませんでした。エラー「Program received signal 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 *image=(UIImage *)[cell viewWithTag:120];

viewWithTag:ビュー(この場合は、)を返します。UIImageViewこれは、のように扱っていますUIImage

UIImageから取得する必要がありますUIImageView。あなたはこのようにそれを行うことができます:

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

または次のように1行で:

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

クラスで関数を作る

-(uiimage*)getImage{ return _urImage; }

または、class2で使用できます

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

最初にそのオブジェクトを作成し、次にclass1でそのプロパティを設定することにより、2ndVCで画像を設定できます

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

まず第一に、より良い/標準的な命名規則を学ぶようにしてください。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];
}
于 2012-11-30T07:11:01.527 に答える