跨视图控制器传输图像

发布于 2024-11-26 11:50:23 字数 1923 浏览 0 评论 0原文

在我的iPhone应用程序上,我有两个viewController(firstViewController,secondViewController),在firstViewController上,用户可以从相机胶卷中选择一张照片,然后将其显示在imageView中,但是我还需要在secondViewController中显示它,但不知道如何到。

您能否也请深入解释您的答案,因为我对 Objective-C 相当陌生,

这是我的代码:

firstViewController.hfirstViewController.m

#import <UIKit/UIkit.h>

@interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImageView *theImageView;
}

@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;

@end

SecondViewController

#import "firstViewController.h"
#import "secondViewController.h"

@implementation firstViewController

@synthesize theImageView

-(IBAction) selectExistingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    theImageView.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}
// Default Apple code

@end

中没有太多内容,因此我不会费心发布该代码。

On my iPhone app, I have two viewControllers (firstViewController, secondViewController), on firstViewController the user can select a photo from the camera roll and it then displays it in an imageView, however I need to also display it in secondViewController but have no idea how to.

Can you also please explain your answers in-depth as I am fairly new to objective-C

Here's my code:

firstViewController.h

#import <UIKit/UIkit.h>

@interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImageView *theImageView;
}

@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;

@end

firstViewController.m

#import "firstViewController.h"
#import "secondViewController.h"

@implementation firstViewController

@synthesize theImageView

-(IBAction) selectExistingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    theImageView.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}
// Default Apple code

@end

There's not much in secondViewController so I won't bother posting that code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-12-03 11:50:23

在第二个视图控制器中有一个类型的方法

-(void)setImage:(UIImage *)image

在创建视图控制器后,在下面的方法中调用带有图像的方法,如图所示

-(IBAction)switchSecondViewController {
    SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller setImage:theImageView.image]
    [viewcontroller release];
}

Have a method in your second view controller of the type

-(void)setImage:(UIImage *)image

In the method below after you create a viewcontroller call the method with the image as shown

-(IBAction)switchSecondViewController {
    SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller setImage:theImageView.image]
    [viewcontroller release];
}
孤芳又自赏 2024-12-03 11:50:23

您需要在第二个视图中声明一个属性,以便您可以从第一个视图中设置图像,如下所示:

secondaryImageView 是 secondaryView 中的属性,您必须设置它

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
     viewcontroller.secondImageView.image = firstViewImage;
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}

You need to declare a property in second view so you can set the image from your first view some thing like this:

secondImageView is the property in secondView and you have to set it

-(IBAction)switchSecondViewController {

    SecondViewController *viewcontroller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
     viewcontroller.secondImageView.image = firstViewImage;
    [self presentModalViewController:viewcontroller animated:YES];
    [viewcontroller release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文