在视图控制器之间传递图像不能用于呈现模态视图

时间:2017-07-03 05:16:20

标签: ios object

我正在尝试将图像从一个视图控制器(包含集合视图)传递到另一个视图控制器,但它没有发生。我是iOS目标c的新手。任何人都可以告诉我解决方案。 这是我的代码......

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


    DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

    AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIImage *image = cell.imageView.image;

    [davc setImage:image];

    //davc.imageView.image = image;


    [self presentModalViewController:davc animated:YES];
}

在我的第二个视图控制器中......

- (void) setImage:(UIImage *) image{

    UIImage *someImage = [[UIImage alloc]init];
    someImage = image;       // image is passing upto here.. i.e. someImage is being allocated with the image passed from first view controller..

    self.imageView.image = someImage;     // here it is showing nil

}

我使用调试控制台..输出是..

(lldb) po someImage
<UIImage: 0x79eb8590>, {500, 347}

(lldb) po self.imageView.image
 nil

5 个答案:

答案 0 :(得分:1)

您只能将图像未设置图像传递到目标控制器。所以你需要制作如下代码。

在DetailedAlbumViewController中

@property (nonatomic, strong) UIImage *yourImage ;

在DetailedAlbumViewControllerv的viewDidLoad中

[self setImage:self.yourImage];

单击单元格

建议 - 您需要使用didselectItemAtIndexpath而不是didDeselectItemAtIndexPath。

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


    DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

    AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIImage *image = cell.imageView.image;
    davc.yourImage = image

    [self presentModalViewController:davc animated:YES];
}

答案 1 :(得分:0)

请检查您是否有self.imageView

答案 2 :(得分:0)

只需在DetailedAlbumViewController中创建UIImage类型的属性。

   @property (nonatomic, strong) UIImage *someImage ;

接下来将当前图像分配给它,如下面的代码片段所示。

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

UIImage *image = cell.imageView.image;

davc.someImage = image;

[self presentModalViewController:davc animated:YES];
}

答案 3 :(得分:0)

DetailedAlbumViewController标头文件上创建属性,将其命名为selectedImage

@property(nonatomic, strong) UIImage *selectedImage;

更改您的实施,如下所示

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{


    DetailedAlbumViewController *davc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailedAlbumViewController"];

    AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIImage *image = cell.imageView.image;

    davc.selectedImage = image;


    [self presentModalViewController:davc animated:YES];
}

DetailedAlbumViewController实施文件viewWillAppear中更新您的imageView图片。

确保您的imageView不是nil。从您的UIImageView / Storyboard xib连接viewWillAppear并在- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (self.imagView == nil) { //allocate and position the imageView self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; } self.imageView.image = self.selectedImage; }

时分配并定位
ISmsProviderFactory

答案 4 :(得分:0)

UIImage * image = cell.imageView.image;

图片可能是零,您需要确认一次,如果图片即将到来,那么您可以将

{
   let orig = Object.assign({}, Intl.NumberFormat.prototype);
   Object.defineProperty(Intl.NumberFormat.prototype, "format", { value: function(){
      //your logic here     
     console.log(orig);// does remember the original proto
   }, configurable: true } );
}

内部 viewDidAppear而不是viewWillAppear方法。