I’m developing an app that i need to load many images in a view using Xcode 4.5.2
So, At the top, I have a UIView, inside of this UIView, i have UIScrollView to display many images,i’m using this UIScrollView in pagingEnabled mode. As i have many images to load, i know that it cannot be possible to load at once. So, i decided to add or remove if need be.
When the app starts, i’ve loaded three images in UIScrollView, when user passes to image 2,
i want to remove image 0 from UIScrollView.When i try to remove the image 0,i got totally white screen.The funny part is that if i dont try to remove image 0,when user passes to image 3, i want to load a new image(image 4) to UIScrollView, and remove image 1.So far, I’m able to do this. Basically,my apps works just fine, but when i want to remove the first(at index 0)image,I got totally white screen on the screen.
Do you have any idea why i’m getting this behaviour ? I got neither warning,nor error. How is it possible that i’m able to remove all the other images but not the image 0 ?
Thank you.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
static NSInteger currentPage = 0 ;
CGFloat pageWidth = self.scrollView.frame.size.width ;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth ;
NSLog(@"%f",fractionalPage);
if(fractionalPage == currentPage+1)
{
NSLog(@"Swipe Left");
currentPage++;
NSLog(@"Current page :%d",currentPage);
[self setLayoutImages:kLeft] ;
}
else if(fractionalPage+1 ==currentPage)
{
NSLog(@"Swipe Right");
currentPage--;
NSLog(@"Current page :%d",currentPage);
[self setLayoutImages:kRight];
}
}
-(void)setLayoutImages:(Direction )direction
{
int currentPage = self.scrollView.contentOffset.x / scrollViewWidth ;
if(direction == kLeft)
{
self.scrollView.contentSize = CGSizeMake(((currentPage+2) * scrollViewWidth), 350.0f);
if(![self.scrollView viewWithTag:currentPage+1])
{
NSString *imageName = @"iPhone.png" ;
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image ] ;
imageView.tag = currentPage+1 ;
CGRect frame = imageView.frame ;
frame.size.height = imageHeight;
frame.size.width = scrollViewWidth ;
frame.origin.x = (currentPage+1) * scrollViewWidth ;
frame.origin.y = 0 ;
imageView.frame = frame ;
if (currentPage>2) { //To be able to remove the first image,i need to add equal here,but i got white screen.
[[self.scrollView viewWithTag:(currentPage -2)]removeFromSuperview ] ; }
[self.scrollView addSubview:imageView ] ;
}
}
else if(direction == kRight)
{
if(![self.scrollView viewWithTag:currentPage-1] && currentPage >0)
{
NSString *imageName = @"gs.jpeg";
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image ];
imageView.tag = currentPage-1 ;
CGRect frame = imageView.frame ;
frame.size.height = imageHeight ;
frame.size.width = scrollViewWidth ;
frame.origin.x = (currentPage-1)*scrollViewWidth ;
frame.origin.y = 0 ;
imageView.frame = frame ;
[[self.scrollView viewWithTag:currentPage+2]removeFromSuperview ] ;
[self.scrollView addSubview:imageView ];
}
}
}
Try making your views start with a tag of 1 not 0, as 0 is the default for an untagged view if i remember correctly, and this might be causing the problem