2013-08-31 3 views
0

모든 장치에서 실행해야하는 iOS 앱을 만들고 있습니다. 슬라이드 쇼를 만들 때 각 장치에 대한 이미지를 갖고 싶지 않았습니다. 나는 이미지를 생성하는 메소드를 생성한다. 배경 이미지, 메인 싱크 이미지, UILabels 및 Scale 2 개를 적절하게 사용합니다. 모든 장치의 세로 방향에서 완벽하게 작동합니다. 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

관련 문제