在多个UIViewControllers之间切换时如何在UIImageView中保留图像

时间:2014-07-23 04:57:31

标签: ios iphone objective-c xcode image

我试图为iPhone5编写一个非常基本的Xcode(第5版)项目应用程序,它将照片库中的图像加载到启动UIImageView中的UIViewController并且在其他视图控制器面板之间切换时,将该图像保留在UIImageView中(或将其返回)。我遇到的问题是当我从其他UIViewController窗口返回到我的UIImageView主视图控制器时,之前显示的已加载图像已经消失,我最终不得不将其重新加载到UIImageView。在从其他UIImageView窗口返回后,如何使图像保留在UIViewController中?

实施档案......

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (IBAction)btnClick:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    modBuffer = info[UIImagePickerControllerEditedImage];
    self.imageView.image = modBuffer;

    [picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

标头文件......

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImage* modBuffer;
}
@property (strong, nonatomic) IBOutlet UIImageView* imageView;
- (IBAction)btnClicked:(id)sender;

1 个答案:

答案 0 :(得分:0)

我认为您需要为UIImage (nonatomic,retain)创建属性,然后您应该尝试这样做。