0

我正在创建一个需要在所有设备上运行的 iOS 应用程序。创建幻灯片时,我不希望每个设备都有一张图片。我创建了一种生成图像的方法。它需要一个背景图像、主接收器图像和 2 个 UILabel 并相应地缩放。对于所有设备上的纵向方向,它都能完美运行。在 iPad 上,我还需要横向。当我旋转它时出现问题,因为它只是拉伸了图像。我不确定我的视野范围发生了什么。对不起,我的代码太长了。 在此处输入图像描述 在此处输入图像描述

-(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];
}
4

1 回答 1

0

您的视图框架viewDidLoad尚未在方法中正确设置。将与帧大小相关的代码移动到viewWillAppear:方法会更准确。框架的尺寸将在viewWillAppear:viewDidAppear:方法中正确。然后,您可能希望保留一个boolean变量,以便仅在第一次出现时生成图像。

于 2013-08-31T21:53:33.650 回答