0

最近我放弃了在我的应用程序中使用故事板和 xib。在这样做的同时,将图像链接到正确的图像名称非常容易。现在我想在没有这些的情况下链接它们并遇到麻烦。在这里和谷歌搜索了很长一段时间,但没有取得多大成功。

这些图像将用于应用程序启动后的启动画面,以防止白色闪烁(与 IB 完美配合)。在我的 .h 文件中,我这样做了。

@property (nonatomic, retain) UIImageView *splash4InchPortrait;

我如何从我的应用程序中的图像中声明该图像名称,例如 splash4InchPortrait@2x.png ?

这就是我在两个文件中的内容。只是为了让您了解我正在处理的内容。

。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],
              (webView.loading?@"NO":@"YES"));
    }

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

    else {
        // Loading iPhone 3.5"
        [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL],
              (webView.loading?@"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

更新:

使用从我的旧项目合并的代码和来自下面接受的答案的代码(感谢 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:@"Default-568h@2x.png"];
        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;
    }
4

2 回答 2

0

UIImage您可以通过首先创建 a然后将 image 属性设置为该图像来声明@ggrana 指出的UIImageView图像。

所以要么

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

或者

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

您还必须在方法内初始化您的 UIImageView - (void)loadView {}

确保您设置了您的框架,UIImageView并将它们添加为您的UIViewController view.

@2x最后一点:当你定义你的图像时,你不需要指定上面指出的。编译器会根据运行它的设备为您执行此操作。

编辑:好的,尝试将其添加到您的实现文件中。

- (void)loadView {
    [super loadView];

    UIImage *myImage = [UIImage imageNamed:@"splash4InchPortrait@2x.png"];
    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];



}

最后,如果您没有在方法中使用 ARC,请确保释放您的图像dealloc

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

这应该让你开始走上一条坚实的道路。

于 2013-06-03T03:09:25.823 回答
0

我建议您执行以下操作:

  • 创建自定义视图以将您的组件封装在一起。
  • 您的自定义视图可以其他自定义视图组成。

然后,您可以定义一个执行此操作的方法,并从控制器调用它,而不是在所有这些组件上设置 hidden 属性。

模型-视图-控制器模式的精神是控制器是模型和视图之间的薄“胶水”层。它的目的是在它们之间来回分流/翻译信息。

加载图像

注意:您只需要选择图像资源名称 - UIImage 将选择正确的资源(@2x、568h@2x 等)。

关于选择要使用的正确图像:如果您使用 Jetbrains 的 AppCode IDE,您可以执行以下操作:

  • 输入 [UIImage imageNamed:@""] 后按 Ctrl-space 以获取可供选择的图像文件列表。

  • ctrl 单击图像文件名以在 IDE 中呈现它并显示大小、像素深度等。

(您可能也可以使用 Xcode 执行此操作 - 不知道,因为它不是我的主要 IDE)。

于 2013-06-03T03:12:54.443 回答