在ScrollViewSuite有問題。它使用的內存比需要的多。 例如:ScrollViewSuite示例代碼使用比所需內存更多的內存?
- 加載圖像中25%和測量存儲器(4 MB)
- 放大圖像爲100%,並測量存儲器(30 MB)
- 縮小到25%,並測量存儲器( 30 MB) - 爲什麼30?它只能使用4 MB - 這是問題所在。
如何解決問題?
/***********************************************************************************/
/* Most of the work of tiling is done in layoutSubviews, which we override here. */
/* We recycle the tiles that are no longer in the visible bounds of the scrollView */
/* and we add any tiles that should now be present but are missing. */
/***********************************************************************************/
- (void)layoutSubviews
{
[self updateResolution];
NSLog(@"layoutSubviews ");
[super layoutSubviews];
CGRect visibleBounds = [self bounds];
// first recycle all tiles that are no longer visible
for (UIView *tile in [tileContainerView subviews]) {
// We want to see if the tiles intersect our (i.e. the scrollView's) bounds, so we need to convert their
// frames to our own coordinate system
CGRect scaledTileFrame = [tileContainerView convertRect:[tile frame] toView:self];
// If the tile doesn't intersect, it's not visible, so we can recycle it
if (! CGRectIntersectsRect(scaledTileFrame, visibleBounds)) {
[reusableTiles addObject:tile];
[tile removeFromSuperview];
}
}
// calculate which rows and columns are visible by doing a bunch of math.
float scaledTileWidth = [self tileSize].width * [self zoomScale];
float scaledTileHeight = [self tileSize].height * [self zoomScale];
int maxRow = floorf([tileContainerView frame].size.height/scaledTileHeight); // this is the maximum possible row
int maxCol = floorf([tileContainerView frame].size.width/scaledTileWidth); // and the maximum possible column
int firstNeededRow = MAX(0, floorf(visibleBounds.origin.y/scaledTileHeight));
int firstNeededCol = MAX(0, floorf(visibleBounds.origin.x/scaledTileWidth));
int lastNeededRow = MIN(maxRow, floorf(CGRectGetMaxY(visibleBounds)/scaledTileHeight));
int lastNeededCol = MIN(maxCol, floorf(CGRectGetMaxX(visibleBounds)/scaledTileWidth));
// iterate through needed rows and columns, adding any tiles that are missing
for (int row = firstNeededRow; row <= lastNeededRow; row++) {
for (int col = firstNeededCol; col <= lastNeededCol; col++) {
BOOL tileIsMissing = (firstVisibleRow > row || firstVisibleColumn > col ||
lastVisibleRow < row || lastVisibleColumn < col);
if (tileIsMissing) {
UIView *tile = [dataSource tiledScrollView:self tileForRow:row column:col resolution:resolution];
// set the tile's frame so we insert it at the correct position
CGRect frame = CGRectMake([self tileSize].width * col, [self tileSize].height * row, [self tileSize].width, [self tileSize].height);
[tile setFrame:frame];
[tileContainerView addSubview:tile];
// annotateTile draws green lines and tile numbers on the tiles for illustration purposes.
[self annotateTile:tile];
}
}
}
// update our record of which rows/cols are visible
firstVisibleRow = firstNeededRow; firstVisibleColumn = firstNeededCol;
lastVisibleRow = lastNeededRow; lastVisibleColumn = lastNeededCol;
}