在我的项目中,我有一个UITableView视图,如果单击该单元格,它将转到详细信息页面。在详细页面中,它有一个显示图像的UIScrollView。
我面临的问题是来自网址的图片没有显示。它只显示占位符图像。但是在我将其单击回UITableView视图并再次单击相同的单元格之后。它将显示URL中的图像。
这是我的代码
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
self.scrollView.pagingEnabled = YES;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
//Fit the Screen
//newPageView.contentMode = UIViewContentModeScaleToFill;
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
newPageView.userInteractionEnabled = YES;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
UITapGestureRecognizer *SingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionHandleTapOnImageView:)];
[SingleTap setNumberOfTapsRequired:1];
[newPageView addGestureRecognizer:SingleTap];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad {
[super viewDidLoad]
// Add image into slide image
if([data.gallery count] == 1){
_imageView1 = [[UIImageView alloc] init];
[_imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", data.gallery[0]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
self.pageImages = [[NSMutableArray alloc] initWithObjects:_imageView1.image, nil];
}else if([data.gallery count] == 2){
// Load image from server
_imageView1 = [[UIImageView alloc] init];
[_imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", data.gallery[0]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
self.pageImages = [[NSMutableArray alloc] initWithObjects:_imageView1.image, nil];
_imageView2 = [[UIImageView alloc] init];
[_imageView2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", data.gallery[1]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
// Add image to page
self.pageImages = [[NSMutableArray alloc] initWithObjects:_imageView1.image,_imageView2.image, nil];
}
}
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}