2013-06-03 60 views
0

最近我在我的應用程序中使用故事板和xibs進行了切割。雖然這樣做很容易將圖像鏈接到正確的圖像名稱。現在我想把它們聯繫起來,沒有那些麻煩。在這裏和谷歌搜索相當一段時間沒有太大的成功。沒有界面生成器的鏈接圖像

這些圖像將用於應用程序啓動後啓動屏幕以防止白色閃光(與IB一起工作完美)。在我的.h文件中,我做到了。

@property (nonatomic, retain) UIImageView *splash4InchPortrait; 

我將如何從圖像我有我的應用程序宣告形象的名字說,[email protected]

這是我在這兩個文件中都有的。只是想讓你知道我在做什麼。

.H

@interface CenterViewController : UIViewController <PullToRefreshViewDelegate, UIWebViewDelegate, UIGestureRecognizerDelegate, MTStatusBarOverlayDelegate, UIScrollViewDelegate> 
{ 
    UIWebView *webView; 
    NSTimer *timer; 
    UIScrollView *currentScrollView; 
    MTStatusBarOverlay *overlay; 

} 


@property(nonatomic) UIViewAutoresizing autoresizingMask; 
@property(nonatomic) UIViewAutoresizing bounds; 


@property (nonatomic, retain) UIImageView *splash35InchPortrait; 
@property (nonatomic, retain) UIImageView *splash35InchLandscape; 
@property (nonatomic, retain) UIImageView *splashRetina35InchPortrait; 
@property (nonatomic, retain) UIImageView *splashRetina35InchLandscape; 
@property (nonatomic, retain) UIImageView *splash4InchPortrait; 
@property (nonatomic, retain) UIImageView *splash4InchLandscape; 
@property (nonatomic, retain) UIImageView *splashPadPortrait; 
@property (nonatomic, retain) UIImageView *splashPadLandscape; 

@end 

.M

@interface UIViewController() 
@end 

BOOL isPortraitView = YES; 

@implementation CenterViewController 

@synthesize splash35InchPortrait, splash35InchLandscape, splashRetina35InchPortrait, splashRetina35InchLandscape, splash4InchPortrait, splash4InchLandscape, splashPadPortrait, splashPadLandscape; 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.f, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    [self.view setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)]; 
    [(UIWebView*)webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com"]]]; 

    webView.contentMode = UIViewContentModeScaleToFill; 
    webView.scalesPageToFit = YES; 
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 
    [self.view addSubview:webView]; 

    for (UIView* subView in webView.subviews) { 
     if ([subView isKindOfClass:[UIScrollView class]]) { 
      currentScrollView = (UIScrollView *)subView; 
      currentScrollView.delegate = (id) self; 

     } 
    } 
    webView.scrollView.scrollsToTop = YES; 
    [webView setDelegate:self]; 
    webView.tag = 999; 

    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(tick) userInfo:nil repeats:YES]; 

    [self.navigationController setNavigationBarHidden:YES]; 
    [super viewDidLoad];  
    webView.contentMode = UIViewContentModeScaleAspectFit; 
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 


//get screen size 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
    if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){ 
     //DO Portrait 
     if (screenBounds.size.height <=480) { 
      //code for 3.5-inch screen 
      splash35InchPortrait.hidden = NO; 
      splash35InchLandscape.hidden = YES; 
      splashRetina35InchPortrait.hidden = YES; 
      splashRetina35InchLandscape.hidden = YES; 
      splash4InchPortrait.hidden = YES; 
      splash4InchLandscape.hidden = YES; 
      splashPadLandscape.hidden = YES; 

     }else{ 
      // code for 3.5 Retina inch screen 
      splash35InchLandscape.hidden = YES; 
      splashRetina35InchPortrait.hidden = NO; 
      splashRetina35InchLandscape.hidden = YES; 
      splash4InchLandscape.hidden = YES; 
      splashPadLandscape.hidden = YES; 
     } 

    }else{ 
     // code for 4-inch screen 
     splash35InchPortrait.hidden = YES; 
     splashRetina35InchPortrait.hidden = YES; 
     splash4InchPortrait.hidden = NO; 
     splash4InchLandscape.hidden = YES; 
     splashPadLandscape.hidden = YES; 
    } 



// Set up Pull to Refresh code 
    PullToRefreshView *pull = [[PullToRefreshView alloc] initWithScrollView:currentScrollView]; 
    [pull setDelegate:self]; 
    pull.tag = 998; 
    [currentScrollView addSubview:pull]; 

//Statusbar Overlay 
    overlay = [MTStatusBarOverlay sharedInstance]; 
    overlay.delegate = self; 
    overlay.progress = 0.0; 
    webView.backgroundColor = [UIColor colorWithWhite:0.1f alpha:1.0f]; 

// Forward/Back Gestures 
    UISwipeGestureRecognizer *goForward = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)]; 
    goForward.direction = UISwipeGestureRecognizerDirectionRight; 
    goForward.delegate = self; 
    goForward.numberOfTouchesRequired = 1; 
    [webView addGestureRecognizer:goForward];  

    UISwipeGestureRecognizer *goBack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)]; 
    goBack.direction = UISwipeGestureRecognizerDirectionLeft; 
    goBack.delegate = self; 
    goBack.numberOfTouchesRequired = 1; 
    [webView addGestureRecognizer:goBack]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
    return NO; 
} 


//Other Gestures 
- (void)swipeLeftAction:(id)ignored{ 
    [webView goBack]; 
    [overlay postMessage:@"Go Back" duration:1 animated:YES]; 
} 
- (void)swipeRightAction:(id)ignored{ 
    [webView goForward]; 
    [overlay postMessage:@"Go Forward" duration:1 animated:YES]; 
} 
-(void)pullToRefreshViewShouldRefresh:(PullToRefreshView *)view { 
    [(UIWebView *)[self.view viewWithTag:999] reload]; 
    [overlay postMessage:@"Reloading" duration:1 animated:YES]; 
} 



//Webview Start/Finish Request 

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
} 


- (void)webViewDidFinishLoad:(UIWebView *)webview { 
#define IS_IPHONE (!IS_IPAD) 
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) 

    bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136)); 
    if (isiPhone5) { 
     // Loading iPhone 5 
     [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading]; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
     NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL], 
       ([email protected]"NO":@"YES")); 
    } 

    else if (IS_IPAD) { 
     // Loading IPAD 
     [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading]; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
     NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL], 
       ([email protected]"NO":@"YES")); 
    } 

    else { 
     // Loading iPhone 3.5" 
     [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading]; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
     NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL], 
       ([email protected]"NO":@"YES")); 
    } 

} 


//Rotation 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){ 
     [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"]; 

    } 
    else{ 
     [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"]; 
    } 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

//Image animation 

- (void)tick { 
    if (!webView.loading) { 
     [self performSelector:@selector(fadeimage) withObject:nil afterDelay:0.3]; 
    } 
} 

-(void)fadeimage{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    splash35InchPortrait.alpha = 0; 
    splash35InchLandscape.alpha = 0; 
    splashRetina35InchPortrait.alpha = 0; 
    splashRetina35InchLandscape.alpha = 0; 
    splash4InchPortrait.alpha = 0; 
    splash4InchLandscape.alpha = 0; 
    splashPadLandscape.alpha = 0; 
    splashPadPortrait.alpha = 0; 
    [UIView commitAnimations]; 
} 


- (void)viewDidUnload { 
    webView = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 


@end 

UPDATE:

隨着代碼從我的舊項目和代碼合併從下面(感謝kushyar)接受的答案。我設法得到它。如果有人好奇,這就是我所做的。

//Setting Splash Images 
    #define IS_IPHONE (!IS_IPAD) 
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) 

    bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136)); 
    if (isiPhone5) { 
     // Load iPhone 5 Splash 
     UIImage *splash4Inch = [UIImage imageNamed:@"[email protected]"]; 
     self.splash4InchPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 320.0f, 568.0f)]; 
     [self.splash4InchPortrait setImage:splash4Inch]; 
     [self.view addSubview:self.splash4InchPortrait]; 
     [self.view bringSubviewToFront:self.splash4InchPortrait]; 
     self.splash4InchPortrait.contentMode = UIViewContentModeScaleToFill; 
    } 

    else if (IS_IPAD) { 
     // Load IPAD StoryBoard 
     UIImage *splashPad = [UIImage imageNamed:@"Default-Portrait~ipad.png"]; 
     self.splashPadPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 768.0f, 1024.0f)]; 
     [self.splashPadPortrait setImage:splashPad]; 
     [self.view addSubview:self.splashPadPortrait]; 
     [self.view bringSubviewToFront:self.splashPadPortrait]; 
     self.splashPadPortrait.contentMode = UIViewContentModeScaleToFill; 
    } 

    else { 
     // Load the iPhone 3.5" Splash 
     UIImage *splash35Inch = [UIImage imageNamed:@"Default.png"]; 
     self.splash35InchPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 320.0f, 480.0f)]; 
     [self.splash35InchPortrait setImage:splash35Inch]; 
     [self.view addSubview:self.splash35InchPortrait]; 
     [self.view bringSubviewToFront:self.splash35InchPortrait]; 
     self.splash35InchPortrait.contentMode = UIViewContentModeScaleToFill; 
    } 

回答

0

您聲明形象首先創建一個UIImage後的UIImageView圖像屬性設置爲圖片中指出@ggrana。

所以無論

UIImage *myImage = [UIImage imageNamed:@"splash4InchPortrait"]; 
[splash4InchPortrait setImage:myImage]; 

[splash4InchPortrait setImage:[UIImage imageNamed:@"splash4InchPortrait"]]; 

您也可以initalize的- (void)loadView {}方法內您的UIImageView的。

確保您設置了您的UIImageView的框架,並將它們添加爲您的UIViewControllerview的子視圖。

最後一個注意事項:如上所述,當您定義圖像時,不需要指定@2x。編譯器會根據它運行的設備爲你做這些事情。

編輯:好吧嘗試添加到您的實施文件。

- (void)loadView { 
    [super loadView]; 

    UIImage *myImage = [UIImage imageNamed:@"[email protected]"]; 
    self.splash4InchPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 568.0f)]; 
    [self.splash4InchPortrait setImage:myImage]; 
    [self.view addSubview:self.splash4InchPortrait]; 
    [self.view bringSubviewToFront:self.splash4InchPortrait]; 



} 

最後,確保你,如果你沒有在dealloc方法使用ARC釋放你的形象。

- (void)dealloc { 
     [self.splash4InchPortrait release]; 
} 

這應該讓你開始一條堅實的道路。

+0

因爲我剛剛使用只是代碼而勉強和我在一起。爲了做到這一點,我需要改變什麼?上面提供的代碼。 – ChrisOSX

+0

所以在你的實現文件(.m)中你有viewDidLoad方法。你可以在它內部或按照我的建議做,使用 - (void)loadView {}方法。爲了清楚起見,我將修改我的帖子。給我一點時間。 – kushyar

+0

不走,仍然沒有出現。我只是試圖讓Default-568h圖像現在顯示。多數民衆贊成我用我的啓動畫面。正如你在代碼中看到的,我有很多其他的啓動畫面。 – ChrisOSX

0

我會建議您做到以下幾點:

  • 創建一個自定義視圖來封裝您的組件連接在一起。
  • 您的自定義視圖可以由組成其他自定義視圖。

然後,而不是設置所有這些組件的隱藏屬性,您可以定義一個單獨的方法,並從控制器調用它。

模型 - 視圖 - 控制器模式的精神在於控制器是模型和視圖之間薄薄的「粘合」層。它的目的是在它們之間來回分流/翻譯信息。

加載圖片

注意:您只需要選擇圖像資源的名字 - 的UIImage會選擇正確的資源(@ 2倍,568h @ 2X等)。

關於選擇使用正確的圖像:如果從Jetbrains的使用AppCode IDE,你可以做到以下幾點:

    打字後
  • 按Ctrl空間[UIImage的imageNamed:@「」]得到一個可供選擇的圖像文件列表。在圖像文件名

  • 按住Ctrl鍵單擊,以使其在IDE和顯示尺寸,像素深度等

(您可以也可能在Xcode做到這一點 - 不知道,因爲它不是我的主要IDE)。