Preserve image selected from UIImagePickerController
I am stuck in a strange situation. In my scenario I have 4 different View Controllers. What I want is to change the RootViewController's background image by giving user the facility to select from UIImagePickerController
in another view. 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. I want to retain the user selected background as much as he is using my app. Kindly guide me the r开发者_StackOverflowight 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. Or if you are, then when it does a viewDidUnload and then viewDidLoad, the bg image is not being preserved.
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.
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 dumpViewToLog:rootViewController.view];
Here's the class:
InspectView.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
#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];
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). You'll want to retain the image and hold a reference to it in your appdelegate.
If it isn't clear what I mean here's some code.
AppDelegate Additions
@interface AppDelegate
//Stuff that's already here.
@property (nonatomic, retain) UIImage *backgroundImage;
@end
View controller additions
@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
精彩评论