久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 UIScrollView 的子視圖中繪制網格會分配巨大的內

Drawing a grid in UIScrollView#39;s subview allocates huge memory(在 UIScrollView 的子視圖中繪制網格會分配巨大的內存)
本文介紹了在 UIScrollView 的子視圖中繪制網格會分配巨大的內存的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在 UIScrollView 中創建一個 UIView,它只包含一個簡單的網格(行和列),由 UIBezierPath 或使用 CG 函數淹沒.問題是,當我有較大的 UIScrollView 內容大小(以及較大的子視圖)時,在繪制網格期間分配了大量內存(50MB 或更多).

I'm trying to create a UIView in UIScrollView that contains just a simple grid (lines as rows and columns) drown by UIBezierPath or using the CG functions. The problem is, that when I have larger content size of the UIScrollView (as well as the larger subview), during the drawing of the grid huge amount of memory is allocated (50MB or more).

UIViewController 僅包含整個場景的 UIScrollView - 在 vi??ewDidLoad 中添加子視圖:

UIViewController which includes just UIScrollView over whole scene - adding subview in viewDidLoad:

@interface TTTTestViewController()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation TTTTestViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    // create the subview
    TTTTestView *testView = [[TTTTestView alloc] init];
    [self.scrollView addSubview:testView];

    //set its properties
    testView.cellSize = 50;
    testView.size = 40;

    // set the content size and frame of testView by the properties
    self.scrollView.contentSize = CGSizeMake(testView.cellSize * testView.size, testView.cellSize * testView.size);
    testView.frame = CGRectMake(0, 0,  self.scrollView.contentSize.width, self.scrollView.contentSize.height);

    // let it draw the grid
    [testView setNeedsDisplay];
}

@end

僅使用 UIBezierPath/CG 函數繪制網格的內部視圖 - 取決于屬性大小(行數/列數)和 cellSize(網格中一個單元格的寬度/高度):

Inner view that just draw the grid using UIBezierPath/CG functions - depends on properties size(rows/columns count) and cellSize (width/height of one cell in grid):

#define GRID_STROKE_WIDTH 2.0

@implementation TTTTestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self drawGrid];
}

-(void)drawGrid
{
    UIBezierPath *path = [[UIBezierPath alloc] init];

    for (int i = 1; i < self.size; i++) {
            //draw row line
        [path moveToPoint:CGPointMake(0, self.cellSize * i)];
        [path addLineToPoint:CGPointMake(self.bounds.size.width, self.cellSize * i)];
            // draw column line
        [path moveToPoint:CGPointMake(self.cellSize * i, 0)];
        [path addLineToPoint:CGPointMake(self.cellSize * i , self.bounds.size.height)];
    }
    [path setLineWidth:GRID_STROKE_WIDTH];
    [[UIColor blackColor] setStroke];
    [path stroke];
    /*
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGContextSetLineWidth(context, GRID_STROKE_WIDTH);
     CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

     for (int i = 1; i < self.size; i++) {
     //draw row line
     CGContextMoveToPoint(context, 0, self.cellSize * i );
     CGContextAddLineToPoint(context, self.bounds.size.width, self.cellSize * i);
     // draw column line
     CGContextMoveToPoint(context, self.cellSize * i , 0);
     CGContextAddLineToPoint(context, self.cellSize * i , self.bounds.size.height);
     }

     CGContextStrokePath(context);
     */
}

@end

示例 1:self.size 為 10,self.cellSize 為 200 => contentSize 為 2000x2000 點以及內部視圖的框架 => 18 行被淹沒,它分配了 ~60MB 內存

Example1: self.size is 10, self.cellSize is 200 => contentSize is 2000x2000 points as well as frame of inner view => 18 lines are drown and it allocates ~60MB memory

示例 2:self.size 為 30,self.cellSize 為 70 => contentSize 為 2100x2100 點以及內部視圖框架 => 58 行被淹沒,它分配了 ~67MB 內存

Example2: self.size is 30, self.cellSize is 70 => contentSize is 2100x2100 points as well as frame of inner view => 58 lines are drown and it allocates ~67MB memory

我在調試繪圖方法時可以看到這些內存編號.無論我如何畫線,調用 [path stroke] 時都會分配大量內存.CGContextStrokePath(上下文).在儀器中,我可以看到最大的內存分配:

These memory numbers I can see when debug the drawing method. No matter how I draw the lines, huge amount of memory is allocated when calling [path stroke] resp. CGContextStrokePath(context). In instruments I can see the biggest memory allocation at line:

12658 0x10200000 VM:CoreAnimation 00:04.092.149 ? 67,29 MB QuartzCore CA::Render::Shmem::new_shmem(unsigned long)

12658 0x10200000 VM: CoreAnimation 00:04.092.149 ? 67,29 MB QuartzCore CA::Render::Shmem::new_shmem(unsigned long)

我是 iOS 編程的新手,我到處尋找解決方案,但我仍然不知道 :-/誰能幫我找到一些解釋這里發生了什么?謝謝:)

I'm quite new in iOS programming and I was searching the solution everywhere and I still have no idea :-/ Can anyone please help me find some explanation what is going on here? Thanks :)

推薦答案

在蘋果開發者論壇上詢問后發現,這實際上是正確分配的內存.這是因為任何使用 -drawRect: 進行繪制的視圖都會使用 (bounds.size.width * bounds.size.height * contentScale * contentScale * 4) 字節的內存.

After asking on apple developer forum, I find out, that this is properly allocated memory in fact. It's because any view that uses -drawRect: to draw will use memory on the order of (bounds.size.width * bounds.size.height * contentScale * contentScale * 4) bytes.

創建避免這種情況的網格的最簡單方法是使用為每一行添加一個視圖并使用視圖的 backgroundColor 屬性為視圖著色.這幾乎不會占用任何內存,因為視圖(可以是普通的 UIView)不需要調用 -drawRect:,因此不會使用額外的內存來存儲繪圖結果.

The simplest way to create a grid that avoids that is to use add a view for each line and use the view's backgroundColor property to color the view. This will use hardly any memory because the view's (which can be plain UIViews) don't need to call -drawRect:, and thus won't use extra memory to store the results of your drawing.

這篇關于在 UIScrollView 的子視圖中繪制網格會分配巨大的內存的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 亚洲乱码国产乱码精品精的特点 | 天天操天天拍 | 欧美a区 | 亚洲免费影院 | 亚洲福利av | 欧美日一区二区 | 中文字幕高清在线 | 久热免费 | 日韩精品一区二区三区在线播放 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 欧美成人高清视频 | 欧美精品久久久久久久久久 | 欧美中文一区 | 99精品国产一区二区青青牛奶 | 国产 欧美 日韩 一区 | 欧美xxxx色视频在线观看免费 | 国产精品永久免费观看 | 亚洲欧美一区二区三区国产精品 | 久久国产成人 | 中文字幕男人的天堂 | 亚洲a级| 免费黄色片在线观看 | 一级片子| 亚洲视频免费在线观看 | 精品成人免费视频 | 伊人爽| 中文字幕人成乱码在线观看 | 精品视频亚洲 | 99re99| 欧美精品网 | 神马久久春色视频 | 成人av网站在线观看 | 日韩第一区 | 91中文字幕在线 | 国产成人99久久亚洲综合精品 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 九九99精品 | 欧美一区二区在线观看 | 国产精品久久国产精品 | 久久不卡日韩美女 | www.玖玖玖 |