0

我成功地从图库中获取了一张图片,它出现在第二个屏幕上。但是当我点击完成按钮时,这个图片被发送到上一个屏幕。

下面是详细解释

1)第一个屏幕最初是一个图像。
2)当我点击这张图片时,它会导航到第二个屏幕,并要求从图库
中选择 3)我是从图库中选择的一张图片,这张图片出现在第二个屏幕中。
4)当我在第二个屏幕上单击完成按钮时,它将导航到上一个屏幕,即第一个屏幕,图像将随所选图像而改变

如何将我的图像传递到上一个屏幕并使用所选图像进行设置。

MprofileViewController.h

#import <UIKit/UIKit.h>
#import "AddProfileViewController.h"
@class MProfileViewController;

@interface MProfileViewController : UIViewController<UIImagePickerControllerDelegate,UITableViewDataSource,UITableViewDelegate,ImageSelectionDelegate>
{    
    NSMutableArray* titles;
    IBOutlet UITableView *mainTableView;
    IBOutlet  UIImageView *image2;
}
@property(strong,nonatomic)IBOutlet UIImageView *image2;


@property(nonatomic, retain) NSMutableArray *titles;
@property(strong,nonatomic)UITableView *mainTableView;

-(IBAction) clickEventOnImage:(id) sender;

@end

MprofileViewController.m

#import "MProfileViewController.h"

@interface MProfileViewController ()
@end

@implementation MProfileViewController

@synthesize titles,mainTableView;
@synthesize image2;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    titles=[[NSMutableArray alloc]init];

    self.navigationItem.title = @"View Profile";
 image2.image=[UIImage imageNamed:@"hariku-indah.jpg"];
}

- (void) imageSelected:(UIImage *)image {
    // Use image
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction) clickEventOnImage:(id) sender{
    AddProfileViewController *Avc = [[AddProfileViewController alloc]initWithNibName:@"AddProfileViewController" bundle:nil];
    Avc.delegate=self;
    [self.navigationController pushViewController:Avc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];

}
@end

AddProfileViewController.h

 #import <UIKit/UIKit.h>

@protocol ImageSelectionDelegate <NSObject>
- (void) imageSelected:(UIImage*)image;
@end

@interface AddProfileViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>{

    IBOutlet UIImageView *imageView;
    NSData *dataImage;
}

// Delegate property
@property (nonatomic,assign) id<ImageSelectionDelegate> delegate;

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)back:(id)sender;
-(IBAction)done:(id)sender;

@end

AddProfileViewController.m

#import "MProfileViewController.h"

@interface AddProfileViewController ()
@property(strong,nonatomic) UIImagePickerController *imagePicker;

@end

@implementation AddProfileViewController
@synthesize imageView;

#pragma mark -
#pragma mark View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"actionSheet");
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0) {
        [self pushTakePhotoScreenInDelegate];
    }
    else if (buttonIndex == 1) {
        [self pushChoosePhotoScreenInDelegate];
    }

}
-(void)pushTakePhotoScreenInDelegate
{
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"viewfinder_2.png"]];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    [imageView setFrame:CGRectMake(0, -52/2.0, screenSize.width, screenSize.height)];
    self.imagePicker.cameraOverlayView = imageView;
    self.imagePicker.delegate = self;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

-(void)pushChoosePhotoScreenInDelegate
{
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.delegate = self;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}


//-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    self.imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];

}

// In case you are using image picker, this delegate is called once image selection is complete.

//- (void)imagePickerController:(UIImagePickerController *)picker
//didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
    //Use either according to your setting, whether you allow image editing or not.
    //self.imageView.image = image;
//UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    //For edited image
   // UIImage *myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    //if([_delegate respondsToSelector:@selector(imageSelected:)]) {
     //   [self.delegate imageSelected:myImage];
   // }
//}


 /*
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    AddProfileViewController * controller = [AddProfileViewController new];
    controller.imageView.image = pickedImage;
   // [self.navigationController pushViewController:controller animated:YES];
}*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [self.navigationController dismissModalViewControllerAnimated:YES];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)viewDidLoad
    {
        [super viewDidLoad];
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..."
        delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo", @"Choose from library", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
        [actionSheet showInView:self.view];
    }

-(IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)done:(id)sender
{
//   if([_delegate respondsToSelector:@selector(imageSelected:)]) {
//        [self.delegate imageSelected:imageView];
//    }

    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
}
@end
4

2 回答 2

-1

在您的 AddprileViewController.h 文件中创建 UIimage 对象为

 UIImage *_gImage;

并将属性设置为

@property (strong, nonatomic) UIImage *_gImage;

在 .m 文件中合成这个对象

  @synthesize _gImage;

在 _gImage 对象中设置图像

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
self.imageView.image = image;

//add image in globle object
_gImage = image;

[picker dismissViewControllerAnimated:YES completion:nil];

}

在你的 MprofileViewController.m

- (void)viewWillAppear:(BOOL)animated {
 if(!Avc) {
 Avc = [[AddProfileViewController alloc]initWithNibName:@"AddProfileViewController" bundle:nil];
 }
if (Avc._gImage != nil) {
 image2.image=Avc._gImage;
}
else {
 image2.image=[UIImage imageNamed:@"hariku-indah.jpg"];
}

}

希望这能解决您的问题。

将此设置为全局对象。

  Avc = [[AddProfileViewController alloc]initWithNibName:@"AddProfileViewController" bundle:nil];
于 2013-08-26T12:14:18.343 回答
-1

在您的应用程序委托中创建一个变量并像这样合成它

在 AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    UIImage *myImage;
}

@property (nonatomic, retain) UIImage *myImage;

在 AppDelegate.m

@implementation AppDelegate

@synthesize myImage;

然后在 AddprileViewController.m 的视图控制器中创建 AppDelegate 实例

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    AddProfileViewController * controller = [AddProfileViewController new];
    controller.imageView.image = pickedImage;

   AppDelegate *appDel=(AppDelegate *)[UIApplication SharedApplication].delegate;
     appDelegate.myImage=pickedImage;
}

现在选择的图像存储在 myImage 变量中,现在你可以在任何你想要的地方使用它

于 2013-08-26T12:17:02.397 回答