2012-11-30 136 views
0

我需要將圖像從一個視圖傳遞到另一個視圖。在第一個視圖中,我從服務URL獲取base64數據,並將其轉換爲特定圖像並顯示在tableviewcell.Now中,當我點擊特定單元格時,相應的圖像應的firstView傳遞給secondView.But無法得到it.getting錯誤「計劃接收信號SIGABRT」在xcode中將圖像從一個視圖傳遞到另一個視圖?

這是代碼我米工作:

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

我怎樣才能得到它?

回答

0

此行是錯誤的:

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]; 
0

使類的功能

-(uiimage*)getImage{ return _urImage; } 

或者 您可以在Class2中

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

使用 並且可以通過創建它的第一個對象,然後設置其設置在2ndVC圖像property 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。

0

首先嚐試學習更好/標準命名約定。 其次,由於您正在將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]; 
} 
相關問題