简体   繁体   English

保留从 UIImagePickerController 中选择的图像

[英]Preserve image selected from UIImagePickerController

I am stuck in a strange situation.我陷入了一个奇怪的境地。 In my scenario I have 4 different View Controllers.在我的场景中,我有 4 个不同的视图控制器。 What I want is to change the RootViewController's background image by giving user the facility to select from UIImagePickerController in another view.我想要的是通过在另一个视图中为用户提供从UIImagePickerController到 select 的设施来更改 RootViewController 的背景图像。 I accomplish it by the standard method and it works fine.我通过标准方法完成它,它工作正常。 But as soon as I go to any other view and come back to the root view, my newly set background image is gone.但是一旦我 go 到任何其他视图并返回根视图,我新设置的背景图像就消失了。 I want to retain the user selected background as much as he is using my app.我想尽可能多地保留用户选择的背景,因为他正在使用我的应用程序。 Kindly guide me the right way that how I can retain the image selected by the user and use it anywhere in my app although the user is going to other views also.请指导我如何保留用户选择的图像并在我的应用程序中的任何位置使用它的正确方法,尽管用户也将访问其他视图。

Regards.问候。

It sounds like when you're setting the bg image, you're not actually setting it on the rootViewController.听起来当您设置 bg 图像时,您实际上并没有在 rootViewController 上设置它。 Or if you are, then when it does a viewDidUnload and then viewDidLoad, the bg image is not being preserved.或者,如果您是,那么当它执行 viewDidUnload 和 viewDidLoad 时,不会保留 bg 图像。

You should put the image the user selected somewhere that rootViewController can access it when it's viewDidLoad method is called, and re-set it when this method is called.您应该将用户选择的图像放在 rootViewController 在调用 viewDidLoad 方法时可以访问它的位置,并在调用此方法时重新设置它。

Here is some handy code for seeing what the subviews of a given view are, so you can see what's actually inside of a view.这是一些方便的代码,用于查看给定视图的子视图是什么,因此您可以查看视图内部的实际内容。

Drop the code below into a new class called InspectView and invoke it like this:将下面的代码放入名为 InspectView 的新 class 中,并像这样调用它:

 [InspectView dumpViewToLog:rootViewController.view];

Here's the class:这是 class:

InspectView.m检查视图.m

 #import "InspectView.h"
 #define THE_LOG NSLog
 @implementation InspectView
 + (void)dumpViewToLog:(id)viewObj  {
THE_LOG(@"%@", [self dumpViewToString: viewObj] );
 }

 + (NSString *)dumpViewToString:(id)viewObj  {

NSString *s =                    @"\nInspect view hierarchy -----------------------------------" ;
s = [ s stringByAppendingFormat: @"\n original view is (0x%x)", viewObj];

// go up to outtermost view.
while ( [viewObj superview] ) {
    viewObj = [viewObj superview];
}

s = [ s stringByAppendingString:[self dumpViewToString: viewObj level:0] ];
s = [ s stringByAppendingString: @"\nEnd of view hierarchy -----------------------------------"];
return s;
 }

 + (NSString *) dumpViewToString:(id)viewObj level:(int) level {
NSString * s=@"\n";
// indent to show the current level
for (int i = 0; i < level; i++) {
    s = [s stringByAppendingString:@".   "];
}

s = [s stringByAppendingFormat:@"%@ (0x%x): frame:(%f,%f) %f x %f [tag=%d] ", [[viewObj class] description], viewObj,
     ((UIView*)viewObj).frame.origin.x, 
     ((UIView*)viewObj).frame.origin.y,
     ((UIView*)viewObj).frame.size.width,
     ((UIView*)viewObj).frame.size.height,
     ((UIView*)viewObj).tag
      ];  // shows the hex address of input view.
//  s = [s stringByAppendingFormat:@"%@ : ", [[viewObj class] description] ];

id obj = [viewObj superclass];

while (NULL != obj) {
    s = [s stringByAppendingFormat: @"%@ : ", [[obj class] description] ];
    obj = [obj superclass];
}

// recurse for all subviews
for (UIView *sub in [viewObj subviews]) {
    s= [s stringByAppendingString: [self dumpViewToString:sub level:(level + 1)]];
}
return s;
 }

 @end

InspectView.h检查视图.h

#define objectString(anObject) [[anObject description] UTF8String]

 #import <Foundation/Foundation.h>
 #import <CoreFoundation/CoreFoundation.h>

 @interface InspectView : NSObject {}

 + (void)      dumpViewToLog:(id)viewObj  ;
 + (NSString *)dumpViewToString:(id)viewObj;
 + (NSString *)dumpViewToString:(id)viewObj level:(int)level;
 @end

I don't know the specifics (you didn't provide any code) but I'd guess the ViewController with the custom background has it's background set through a UIImageView or.backgroundColor = [UIColor colorWithPatternImage:image];我不知道细节(您没有提供任何代码),但我猜具有自定义背景的 ViewController 的背景是通过 UIImageView 或 .backgroundColor = [UIColor colorWithPatternImage:image]; 设置的。

Either way I'd guess it disappears because your recreating the view and the image's reference was only saved in that object itself (which is now destroyed).无论哪种方式,我猜它都会消失,因为您重新创建视图和图像的参考仅保存在 object 本身(现在已被销毁)。 You'll want to retain the image and hold a reference to it in your appdelegate.您需要保留图像并在您的 appdelegate 中保留对它的引用。

If it isn't clear what I mean here's some code.如果不清楚我的意思是这里的一些代码。

AppDelegate Additions AppDelegate 添加

@interface AppDelegate
//Stuff that's already here.
@property (nonatomic, retain) UIImage *backgroundImage;
@end

View controller additions查看 controller 添加

@implementation ViewController

-(void)viewDidLoad{
    self.view.backgroundImage = ((AppDelegate*)[UIApplication sharedApplication].delegate).backgroundImage;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    ((AppDelegate*)[UIApplication sharedApplication].delegate).backgroundImage = [image retain];  
    //background code here
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM