1

当我单击图像时,我正在更改个人资料图片的第一个屏幕,它将进入下一个屏幕,并在此要求从图库中选择或拍照。假设我从画廊中选择,它进入第二个屏幕并且图像正在出现。我的问题是当我在第二个屏幕上单击完成按钮时,图像会在第一个屏幕上发生变化,即个人资料图片。它在第一个屏幕上将如何变化,请回复我。

4

1 回答 1

0

SecondViewController在课堂上创建一个委托。FirstViewController在课堂上分配和实现这个委托。

当用户从图库中选择图像或单击照片时,调用委托方法将所选图像传递给FirstViewController类。

实现这一点的示例代码如下

SecondViewController.h

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

@interface SecondViewController : UIViewController
// Delegate property
@property (nonatomic,assign) id<ImageSelectionDelegate> delegate;
@end

第二视图控制器.m

@implementation SecondViewController

// 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.
    UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    //For edited image
    //UIImage *myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    if([_delegate respondsToSelector:@selector(imageSelected:)]) {
        [self.delegate imageSelected:myImage];
    }
}

//OR

// Done button click
- (IBAction)doneButtonClick:(id)sender
{
    // You can store the image selected by user in a UIImage pointer.
    // Here I have UIImage *selectedImage as an ivar.

    // Call the delegate and pass the selected image.
    if([_delegate respondsToSelector:@selector(imageSelected:)]) {
        [self.delegate imageSelected:selectedImage];
    }

    // Pop the view controller here
}

@end

第一视图控制器.h

#import "SecondViewController.h"

@interface FirstViewController : UIViewController <ImageSelectionDelegate>

@end

第一视图控制器.m

@implementation FirstViewController
- (void) onImageClick {
    SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    // Assign delegate
    controller.delegate = self;
    [self.navigationController pushViewController:controller animated:YES];
}

// Implement delegate method
- (void) imageSelected:(UIImage *)image {
    // Use image
}
@end
于 2013-06-20T12:58:31.753 回答