将UIImage从一个类转移到另一个类

时间:2014-02-01 02:55:06

标签: ios iphone objective-c

我想从一个类(从服务器中提取)中获取UIImage并在单独的类(不同的View Controller)中使用它。

UIImage *image = [UIImage imageWithData:imageData];

这是我SecondViewController中的当前代码。我怎样才能获得它以便我可以在FirstViewController中使用UIImage image?谢谢!

2 个答案:

答案 0 :(得分:0)

非常简单......在这里尝试NSUserDefaults概念。如果存储在NSUserDefaults中,您可以在应用的任何视图控制器中获取UIImage。

第1步:存储

图像保存在第二个视图控制器中的NSUserDefaults

  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"]; 

第2步:检索

在第一个视图控制器中的NSUserDefaults上检索图像:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
UIImage* image = [UIImage imageWithData:imageData];

答案 1 :(得分:0)

如果您使用.xib从ViewController导航到另一个FirstViewController,则此代码可能会对您有所帮助。

假设您有UIImage *image = [UIImage imageWithData:imageData]; //------Time to go at another Class lets call it secondViewController secondViewController *secondview =[[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil WithImage:image]; [self.navigationController pushViewController:secondview animated:YES];

中的图片
@interface secondViewController : UIViewController

@property (nonatomic,strong)UIImage *imagefromServer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil WithImage:(UIImage *)image;

@end

SecondViewController.h

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

SecondViewController.m

{{1}}

希望帮助你

相关问题