Существует проблема в ScrollViewSuite. он использует больше памяти, чем необходимо. например:Пример кода ScrollViewSuite Использует больше памяти, чем необходимо?
- Загрузка изображения в 25% и измерения памяти (4 MB)
- Увеличить в изображении до 100% и измерить память (30 МБ)
- Уменьшить до 25% и измерения памяти (30 МБ) - почему 30? он должен использовать только 4 МБ - вот в чем проблема.
Как исправить проблему?
/***********************************************************************************/
/* 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;
}