【问题标题】:Send photo to next screen on button press按下按钮时将照片发送到下一个屏幕
【发布时间】:2018-02-11 16:05:58
【问题描述】:

所以我正在构建一个 IOS 应用程序,并且我有一个显示的照片库,当你按下一张照片时,它会注册,你可以用那张照片做一些事情。基本上我需要的是当用户按下那张照片时,我需要它来打开另一个屏幕(segue),我需要知道在那个新屏幕上按下的特定照片是什么,并将屏幕大小的 UIViewController 设置为该照片。我目前拥有的代码是

      PhotoEditorViewController *editor = [[PhotoEditorViewController alloc] init];
ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
editor.img = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:UIImageOrientationRight];
NSLog(@"Photo Pressed");
NSLog(@"%@", editor.img);

[self presentModalViewController:editor animated:YES];

所以目前正在做的是获取按下的照片并设置我的 UIImage var。 “img”作为该图像,然后当调用 segue 时,我可以在其他屏幕上看到图像传输过来的 viewDidLoad 方法,但由于某种原因,我无法将该图像设置为 UIImageViwer。

NSLog 输出:

在屏幕一和屏幕二上都显示 UIImage 0x#######

这让我相信照片 ID 信息正在传输但它没有更新 uiimageview

    ViewDidLoad
        {
             NSLog(@“%hhu%”, editorPhoto);
             [image setImage:editorPhoto];
        }

【问题讨论】:

  • 请在“第二个屏幕”上显示应该是“更新 UIImageView”的 PhotoEditorViewController 代码。
  • @matt 我添加的代码不在家,所以我只添加了重要的代码。因此,当代码运行时,它会打印正确的内容,但如果您将断点设置为 setImage,则会显示图像为空

标签: ios objective-c iphone uiimageview uiimage


【解决方案1】:

使用 'prepare for Segue' 方法在 ViewController 之间传递数据:

在 PhotoEditorViewController 中创建一个 UIImage 来保存传递的图像

@IBOutlet weak var passedImage: UIImageView!

在你的第一个 ViewController 中

Swift 3.0

@IBOutlet weak var selectedImage: UIImageView!

override fun prepare(for segue: UIStorybordSegue, sender: Any?) {
      //set destination to PhotoEditorViewController
      if let destination = segue.destination as? 
      PhotoEditorViewController {

      //send image to 'passedImage' in PhotoEditorViewController
      destination.passedImage = selectedImage.image
}

Objective-C

How to pass prepareForSegue: an object

【讨论】:

  • 你如何在目标 c 中做到这一点
  • 添加了相关文章的链接
  • 再次检查您的 project.storyboard 是否已在视图控制器之间创建了一个 segue,并且您将该 segue 命名为与您尝试调用的 segue 完全相同
【解决方案2】:

在 nextViewController.h 文件中创建属性。

@property (strong, nonatomic) UIImageView *imgView;

从这里传递图像。

- (IBAction)nextView:(UIButton *)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    myViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"myViewControllerID"];
    VC.imgView.image = [UIImage imageNamed:@"image.jpg"];
    [self.navigationController pushViewController:VC animated:YES];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多