簡體   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