1

如何更改我的个人资料图片图像,当我单击第二个屏幕中的完成按钮时,第一个屏幕图像应与第二个屏幕图像一起更改。我正在从图库中选择图像,该图像出现在我的第二个屏幕中,当我单击完成按钮时,如何在第一个屏幕视图控制器中设置此图像。

这是 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

添加prileViewController.m

 #import "AddProfileViewController.h"
#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

1 回答 1

0

// if([_delegate respondsToSelector:@selector(imageSelected:)]) { // [self.delegate imageSelected:imageView]; }

此代码在技术上是正确的。但是您忘记在 AddprofileViewController 中合成您的委托

于 2013-11-05T20:03:29.333 回答