2013-08-31 21 views
0

我正在創建需要在所有設備上運行的iOS應用程序。創建幻燈片時,我不想讓每個設備都有一個圖像。我創建一個方法來生成圖像。它需要一個背景圖像,主水槽圖像和2個UILabel並相應地縮放。對於所有設備上的縱向定位,它可以很好地工作。在iPad上我也需要風景。當我旋轉它時會出現錯誤,因爲它只是拉伸圖像。我不確定發生在我的視野範圍內的事情。 對不起,我的代碼太長了。 enter image description here enter image description here旋轉後iPhone查看大小不正確

-(void)viewDidLoad{ 

    _screenSize = self.view.bounds.size; 
    _slideShowImageArray = [[NSMutableArray alloc] init]; 
    _slideShow = [[NSMutableArray alloc] init]; 

    for (int i = 1; i<=8; i++) { 
     [_slideShow addObject:[self slideNumber:[NSNumber numberWithInt:i]]]; 
    } 
    [self generateImages]; 

    _SlideImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, _screenSize.width, _screenSize.height)]; 
    [[self view]addSubview:_SlideImageView]; 
    [self setSlideNumber:[NSNumber numberWithInt:1]]; 
    [self begin]; 
} 

-(void)begin{ 
    if ([_SlideNumber intValue] <=8) { 

     UIImage * toImage = [_slideShowImageArray objectAtIndex:[_SlideNumber intValue]-1]; 
     [UIView transitionWithView:self.view 
          duration:1.0f 
          options:UIViewAnimationOptionTransitionCrossDissolve 
         animations:^{ 
          self.SlideImageView.image = toImage; 
         } completion:^(BOOL finished) { 
          sleep(2); 
          [self begin]; 
         }]; 


     int SlideNumber = [_SlideNumber intValue]; 
     int NewSlideNumber = SlideNumber+=1; 
     [self setSlideNumber:[NSNumber numberWithInt:NewSlideNumber]]; 
    } 

} 
-(SlideShowObj*) slideNumber:(NSNumber*)number{ 
    SlideShowObj * slideShowOBJ = [[SlideShowObj alloc] init]; 

    UIImageView * counterTempImageView = [[UIImageView alloc]init]; 
    NSString * imageName = [NSString stringWithFormat:@"bg%d.png",[number intValue]]; 
    counterTempImageView.image = [UIImage imageNamed:imageName]; 
    [slideShowOBJ setCounterTopIMG:counterTempImageView]; 


    UIImageView * sinkTempImageView = [[UIImageView alloc]init]; 
    imageName = [NSString stringWithFormat:@"sink%d.png",[number intValue]]; 
    sinkTempImageView.image = [UIImage imageNamed:imageName]; 
    [slideShowOBJ setSinkIMG:sinkTempImageView]; 

    NSString * textAboveSink = @""; 
    NSString * textBelowSink = @""; 
    UIColor * textAboveSinkColor = [UIColor blackColor]; 
    UIColor * textBelowSinkColor = [UIColor blackColor]; 
    if ([number intValue]==1) { 
     textAboveSink = [NSString stringWithFormat:@"8 CAPTIVATING \nCOLORS"]; 
     textBelowSink = [NSString stringWithFormat:@"Anthracite"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    }else if ([number intValue]==2) { 
     textAboveSink = [NSString stringWithFormat:@"TO ENHANCE \nANY DECOR"]; 
     textBelowSink = [NSString stringWithFormat:@"Cinder"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    }else if ([number intValue]==3) { 
     textAboveSink = [NSString stringWithFormat:@"REAL TOUGH,\nFOR REAL LIFE"]; 
     textBelowSink = [NSString stringWithFormat:@"Metallic Grey"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    }else if ([number intValue]==4) { 
     textAboveSink = [NSString stringWithFormat:@"HYGENIC+PLUS \nKEEPS IT CLEAN"]; 
     textBelowSink = [NSString stringWithFormat:@"Café Brown"]; 
     textAboveSinkColor = [UIColor blackColor]; 
     textBelowSinkColor = [UIColor blackColor]; 
    }else if ([number intValue]==5) { 
     textAboveSink = [NSString stringWithFormat:@"HEAT PROOF \nACID RESISTANT"]; 
     textBelowSink = [NSString stringWithFormat:@"Truffle"]; 
     textAboveSinkColor = [UIColor blackColor]; 
     textBelowSinkColor = [UIColor blackColor]; 
    }else if ([number intValue]==6) { 
     textAboveSink = [NSString stringWithFormat:@"SCRATCH RESISTANT \nSTAIN RESISTANT"]; 
     textBelowSink = [NSString stringWithFormat:@"Biscotti"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    }else if ([number intValue]==7) { 
     textAboveSink = [NSString stringWithFormat:@"NON-FADING \nKEEPS IT CLEAN"]; 
     textBelowSink = [NSString stringWithFormat:@"Biscuit"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    }else if ([number intValue]==8) { 
     textAboveSink = [NSString stringWithFormat:@"FOR THE LIFE \nOF THE SINK"]; 
     textBelowSink = [NSString stringWithFormat:@"White"]; 
     textAboveSinkColor = [UIColor whiteColor]; 
     textBelowSinkColor = [UIColor whiteColor]; 
    } 
    [slideShowOBJ setTopTextColor:textAboveSinkColor]; 
    [slideShowOBJ setBottomTextColor:textBelowSinkColor]; 
    [slideShowOBJ setTopText:textAboveSink]; 
    [slideShowOBJ setBottomText:textBelowSink]; 

    return slideShowOBJ; 
} 
-(void)generateImages{ 
    for (int i = 1; i<=8; i++) { 


     UIImage * bgImage = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]counterTopIMG]image]; 
     //find out the width and height ratio of your image. 
     double ratio = bgImage.size.width/bgImage.size.height; 
     //determine the calculate width according the height. here height is set as mainScreen. 
     double calculatedWidth = ratio * _screenSize.height; 
     UIImageView * bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, calculatedWidth, _screenSize.height)]; 
     [[self view] addSubview:bgImageView]; 
     bgImageView.image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]counterTopIMG]image]; 


     UIImage * image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]sinkIMG]image]; 
     //find out the width and height ratio of your image. 
     double ratio1 = image.size.width/image.size.height; 
     //determine the calculate width according the height. here height is set as mainScreen. 
     double calculatedWidth1 = ratio1 * (_screenSize.height)*.5; 
     double calculatedheight1 = ratio1 * _screenSize.width*.5; 
     UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_screenSize.width/2-calculatedWidth1/2, ((_screenSize.height)/2-calculatedheight1/2)*1.3, calculatedWidth1, calculatedheight1)]; 
     [[self view] addSubview:imageView]; 
     imageView.image = [[(SlideShowObj*)[_slideShow objectAtIndex:i-1]sinkIMG]image]; 
     [imageView setContentMode:UIViewContentModeScaleAspectFit]; 
     UIFont *helvFont28 = [UIFont fontWithName:@"Helvetica" size:28.0]; 
     UIFont *helvFont24 = [UIFont fontWithName:@"Helvetica" size:24.0]; 

     UILabel * topText = [[UILabel alloc] initWithFrame:CGRectMake(0, (_screenSize.height)/2-(_screenSize.height)*.4, _screenSize.width, 100)]; 
     [topText setNumberOfLines:2]; 
     [topText setFont:helvFont28]; 
     [topText setTextAlignment:NSTextAlignmentCenter]; 
     [topText setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.2]]; 
     [topText setTextColor:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]topTextColor]]; 
     [[self view]addSubview:topText]; 
     [topText setText:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]topText]]; 

     UILabel * bottomText = [[UILabel alloc] initWithFrame:CGRectMake(0, (_screenSize.height)/2+(_screenSize.height)*.3, _screenSize.width, 40)]; 
     [bottomText setFont:helvFont24]; 
     [bottomText setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.2]]; 
     [[self view]addSubview:bottomText]; 
     [bottomText setText:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]bottomText]]; 
     [bottomText setTextAlignment:NSTextAlignmentCenter]; 
     [bottomText setTextColor:[(SlideShowObj*)[_slideShow objectAtIndex:i-1]bottomTextColor]]; 


     UIGraphicsBeginImageContext(_screenSize); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     [[[self view]layer] renderInContext:context]; 
     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 
     NSLog(@"%f,%f",theImage.size.width, theImage.size.height); 
     [_slideShowImageArray addObject:theImage]; 
     UIGraphicsEndImageContext(); 

     [bottomText removeFromSuperview]; 
     [topText removeFromSuperview]; 
     [imageView removeFromSuperview]; 
     [bgImageView removeFromSuperview]; 

    } 
} 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self reload]; 
} 
-(void)reload{ 

    [_SlideImageView setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, _screenSize.width, _screenSize.height)]; 
    [_slideShow removeAllObjects]; 
    [_slideShow removeAllObjects]; 

    for (int i = 1; i<=8; i++) { 
     [_slideShow addObject:[self slideNumber:[NSNumber numberWithInt:i]]]; 
    } 
    [self generateImages]; 
    [self setSlideNumber:[NSNumber numberWithInt:1]]; 
    [self begin]; 
} 

回答

0

視圖的框架還沒有在viewDidLoad方法的設置是否正確,但。將幀大小相關的代碼移動到viewWillAppear:方法會更準確。框架的尺寸將在viewWillAppear:viewDidAppear:方法中正確。然後,您可能需要保留一個boolean變量,以便僅在第一次出現時生成圖像。

+0

這種情況發生在旋轉時不在啓動 – BDGapps

+0

'_screenSize'設置在'viewDidLoad'中並且旋轉後保持不變。 'reload'在重新生成圖像的同時使用舊的幀。你的代碼有點長,不好意思,如果我錯過了一些東西。 – mkubilayk