繁体   English   中英

保留从 UIImagePickerController 中选择的图像

[英]Preserve image selected from UIImagePickerController

我陷入了一个奇怪的境地。 在我的场景中,我有 4 个不同的视图控制器。 我想要的是通过在另一个视图中为用户提供从UIImagePickerController到 select 的设施来更改 RootViewController 的背景图像。 我通过标准方法完成它,它工作正常。 但是一旦我 go 到任何其他视图并返回根视图,我新设置的背景图像就消失了。 我想尽可能多地保留用户选择的背景,因为他正在使用我的应用程序。 请指导我如何保留用户选择的图像并在我的应用程序中的任何位置使用它的正确方法,尽管用户也将访问其他视图。

问候。

听起来当您设置 bg 图像时,您实际上并没有在 rootViewController 上设置它。 或者,如果您是,那么当它执行 viewDidUnload 和 viewDidLoad 时,不会保留 bg 图像。

您应该将用户选择的图像放在 rootViewController 在调用 viewDidLoad 方法时可以访问它的位置,并在调用此方法时重新设置它。

这是一些方便的代码,用于查看给定视图的子视图是什么,因此您可以查看视图内部的实际内容。

将下面的代码放入名为 InspectView 的新 class 中,并像这样调用它:

 [InspectView dumpViewToLog:rootViewController.view];

这是 class:

检查视图.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

检查视图.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

我不知道细节(您没有提供任何代码),但我猜具有自定义背景的 ViewController 的背景是通过 UIImageView 或 .backgroundColor = [UIColor colorWithPatternImage:image]; 设置的。

无论哪种方式,我猜它都会消失,因为您重新创建视图和图像的参考仅保存在 object 本身(现在已被销毁)。 您需要保留图像并在您的 appdelegate 中保留对它的引用。

如果不清楚我的意思是这里的一些代码。

AppDelegate 添加

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

查看 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