2011-11-30 67 views
1

我想有一個靜態UIImage,所以我可以從不同的類訪問它。我試過這種方法,但沒有工作:靜態UIImage在整個應用程序

製造Constans.h文件有:

static UIImage *myImage; 

此後,我導入這個頭在需要的地方。我認爲在這個時候myImage是靜態的,對這個對象所做的任何更改都是可見的。但它看起來像每個班都在自己的工作myImage實例。有什麼辦法讓這樣的靜態UIImage?


編輯:

物業在AppDelegate的正常工作。我現在有靜態的UIImage,但仍然沒有我期待的效果。

我在ViewController中有一個UIImageView。我將圖像加載到我的delegate.myImage後,我做的事:

delegate.myImage = [UIImage imageNamed:@"blah.png"]; 
myImageView.image = delegate.myImage; 

圖像加載,但之後我想改變它的AppDelegate,但是當我改變MYIMAGE這樣:

delegate.myImage = [UIImage imageNamed:@"blah2.png"]; 

沒有任何變化myImageView。這就像MYIMAGEmyImageView.image = delegate.myImage複製內存地址,這樣以後如果我改變MYIMAGE它不影響myImageView.image的參考。我想要有一個UIImage,任何更改後,它也會影響myImageView

在AppDelegate中有沒有其他方法可以參考myImageView

+0

使其成爲appDelegate類的屬性。因爲appDelegate類對象inSelf是靜態副本。 – Ishu

回答

1

這是一個C的問題(沒有具體涉及到的Objective-C或iOS)

static關鍵詞使其編譯單元內的可變粘稠。

當你#include(或#import在ObjC)的標題,這就像如果其內容被複制&粘貼到包含它的文件。提醒一下,「.h」文件沒有編譯(它們只包含在自己編譯的「.m」文件中)。

因此,它的工作方式與您輸入的代碼行數完全相同在你的.h中的任何文件中都是#include吧。

這解釋了爲什麼在你的每個源文件中他們每個都看到myImage變量的不同實例。


相反,你應該:

  • 使用Singleton模式,專門爲這種情況下
  • 或在實現文件( 「Constants.m」)使用static關鍵字做使這個變量在這個「Constants.m」文件的編譯單元內變得粘稠

I highl y無論如何推薦使用單例模式來處理這種情況More info on Singleton Pattern here in the Apple official DevPedia

0

在iPhone中,AppDelegate類產生靜態類。所以你可以做同樣的事情,你在Constant.h中做的YourAppDelegate類。但不要使用靜態關鍵字。

我不是很確定,但認爲它會工作。 :)

3

關鍵字static使編譯單元的本地變量在id定義。這意味着您可以安全地在多個.c文件中定義相同的符號;所有這些聲明不會相互衝突,並且每個文件都有自己的專用符號。簡單地說,如果你真的想定義一個全局變量,你的程序的任何部分都可以訪問它,那麼你不需要關鍵字static。在這種情況下,雖然,「絕招」是在頭文件中聲明的變量(您包括無處不在的全球應該是可見的)這樣的:

extern UIImage *myImage; 

,然後提供該變量定義在一個單一的放置(.c文件)沒有static關鍵字。 extern關鍵字告訴編譯器該符號的定義不在當前編譯單元(.c文件)中,而是在另一個編譯單元中找到。

現在,正如其他人指出的那樣,儘管通常會認識到使用singleton to mask a global variable is usually a way to mask a design problem,但可以通過單例方式更好地完成此操作。

0

您可以使用UIImage類別爲例來獲取此圖片。

在你的.h文件中只需添加你的靜態方法。

#import <UIKit/UIKit.h> 

@interface UIImage (StaticImage) 

+(UIImage *)staticImage; 

@end 

而你的。M檔執行以下步驟:

#import "UIImage+StaticImage.h" 

//This is your static image 
static UIImage *myStaticImage; 

@implementation UIImage (StaticImage) 

+(void)initialize{ 
    //Important to add this condition, because this method will be called for every 
    //child class of UIImage class 
    if (self == [UIImage class]){ 
     myStaticImage = [[UIImage alloc] init]; 
    } 
} 

+(UIImage *)staticImage{ 
    //Just return your existing static image 
    return myStaticImage; 
} 

@end 
1

您可以在@property (nonatomic, retain) UIImage *image;在您的應用程序委託,並在您要使用可以創建AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];,然後將圖像的每類創建從delegate對象像這樣接觸到的UIImage:

[imageView setImage:[delegate image]]; 

或者你可以使用這樣的類:

頭文件

@interface Data : NSObject 

@property (nonatomic, strong) UIImage *image; 

+ (Data *)sharedInstance; 
+ (id)allocWithZone:(NSZone*)zone; 
- (id)init; 
- (id)copyWithZone:(NSZone *)zone; 

@end 

實現文件

@implementation Data 

@synthesize image; 

static Data *sharedInstance=nil; 

+ (Data *)sharedInstance { 

    if (sharedInstance == nil) { 
     sharedInstance = [[super allocWithZone:NULL] init]; 
    } 

    return sharedInstance; 
} 

+ (id)allocWithZone:(NSZone*)zone { 
    return [self sharedInstance]; 
} 

- (id)init 
{ 
    self = [super init]; 

    if (self) { 

    } 
    return self; 
} 

- (id)copyWithZone:(NSZone *)zone { 
    return self; 
} 

@end 

然後,你必須在你想每一個類導入Data.h,然後使用:

UIImageView *imageView=[[UIImageView alloc] init]; 
[imageView setImage:[[Data sharedInstance] image]]; 

這不是製造一個適合我的偉大工程:)

4

相反明確的應用程序範圍的圖像,只需使用[UIImage imageNamed:]。這爲您處理圖像的緩存!等。無論您需要使用的圖像,只需訪問它,像這樣:

[UIImage imageNamed:@"imageName.png"] 

注:這將導致那裏是圖像的單個拷貝到內存中。您無法卸載它 - 但是在較低的內存條件下,iOS的新版本可能會在後臺卸載它。

另請參閱[UIImage imageNamed:]的API文檔。

順便說一句,imageNamed通常用於多次使用的小圖像 - 例如,表單元格圖像 - 但如果您真的想要靜態應用程序範圍的圖像,則沒有理由不在大圖像上使用它。

相關問題