0

I have Array of images, which I need to display on the Landscapeand portrait mode.. what I have done is that I have created a Array of images and displaying on the scrollView to see one at a time ..but problem is that When I move to Landscape to portrait or vice versa images get split ...view comes with the half previous image and half next.. this does not happen everytime...I dont know why the images are splitting and not exactly in half ...sometime split in different size..

#define portrait_width1 600.0
#define portrait_height1 600.0
#define landscape_width1 800.0
#define landscape_height1 445.0

 CGFloat kScrollObjHeight_por1 = portrait_height1;
CGFloat kScrollObjWidth_por1    = portrait_width1;

- (void)viewDidLoad{
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;
    // default is NO, we want to restrict drawing within our scrollview
    scrollView1.scrollEnabled = YES;
    scrollView1.delegate = self;
    scrollView1.pagingEnabled = YES;
    [scrollView1 setShowsHorizontalScrollIndicator:NO];
    [scrollView1 setShowsVerticalScrollIndicator:NO];
   kNumImages=content.count;
      for (i = 1; i <= content.count; i++)
    {

        NSArray *photos = [NSArray arrayWithObjects:


                            [UIImage imageNamed:@"c.jpg"],
                            [UIImage imageNamed:@"photo2m.jpg"],[UIImage imageNamed:@"photo1m.jpg"],[UIImage imageNamed:@"photo4m.jpg"],[UIImage imageNamed:@"photo2l.jpg"],[UIImage imageNamed:@"photo4l.jpg"],
                            nil] ;

        UIImage *image = [photos objectAtIndex:i];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight_por1;
        rect.size.width = kScrollObjWidth_por1;
        rect.origin.y=scrollView1.frame.origin.y;
        imageView.frame = rect;
        imageView.tag = i;


        [scrollArray addObject:imageView];
        [scrollView1 addSubview:imageView];
        [imageNameArray addObject:[[content objectAtIndex:i-1] _image_url]];

    }

    [self layoutScrollImages];
}
}


 -(void)layoutScrollImages
 {
 @try {
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

        // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    CGRect frame;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth_por1);
        }
    }

        // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];
        // scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
    NSLog(@"%d",[imageNameArray indexOfObject:Images]);
    currentImage=[scrollArray objectAtIndex:[imageNameArray indexOfObject:Images]];

    [scrollView1 scrollRectToVisible:currentImage.frame animated:YES];
}
@catch (NSException *exception) {
    NSLog(@"ImageFullScreenViewController - layoutScrollImages Exception Name = %@ Exception Reason = %@",[exception name],[exception reason]);
}


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

@try {
    if ([UIDevice currentDevice].orientation== UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation  == UIInterfaceOrientationLandscapeRight)
    {
        if (!landscapeImgSCRf) {
            CGFloat curXLoc = 0;
            CGRect frame;
            for (UIView *subview in scrollView1.subviews)
            {


                if ([subview isKindOfClass:[UIImageView class]])
                {
                    if (!rightside2f) {
                        kScrollObjWidth_por1 = landscape_width1;
                        kScrollObjHeight_por1= landscape_height1;
                        curXLoc = subview.frame.origin.x;
                        rightside2f=YES;

                    }
                    frame.origin = CGPointMake(curXLoc, 0);
                    [subview setFrame: CGRectMake(curXLoc, subview.frame.origin.y, kScrollObjWidth_por1, kScrollObjHeight_por1)];
                    curXLoc += (kScrollObjWidth_por1);
                    scrollView1.frame=CGRectMake(scrollView1.frame.origin.x, scrollView1.frame.origin.y, scrollView1.frame.size.width, scrollView1.frame.size.height);

                }


            }


            [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];
            landscapeImgSCRf=YES;
        }

    }else
    {
        if (landscapeImgSCRf) {
            CGFloat curXLoc = 0;
            CGRect frame;
            for (UIView *subview in scrollView1.subviews)
            {


                if ([subview isKindOfClass:[UIImageView class]])
                {
                    if (rightside2f) {
                        kScrollObjWidth_por1 = portrait_width1;
                        kScrollObjHeight_por1= portrait_height1;
                        curXLoc = subview.frame.origin.x;
                        rightside2f=NO;

                    }
                    frame.origin = CGPointMake(curXLoc, 0);
                    [subview setFrame: CGRectMake(curXLoc, subview.frame.origin.y, kScrollObjWidth_por1, kScrollObjHeight_por1)];
                    curXLoc += (kScrollObjWidth_por1);
                    scrollView1.frame=CGRectMake(scrollView1.frame.origin.x, scrollView1.frame.origin.y, scrollView1.frame.size.width, scrollView1.frame.size.height);
                }


            }
            landscapeImgSCRf=NO;
            [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth_por1), kScrollObjHeight_por1)];


        }

    }

}
@catch (NSException *exception) {
    NSLog(@"ImageFullScreenViewController - willRotateToInterfaceOrientation Exception Name = %@ Exception Reason = %@",[exception name],[exception reason]);
}



 }

check for image

user2102546
  • 87
  • 1
  • 7

0 Answers0