2010-09-03 65 views
1

我正在處理的應用程序要求我將圖像繪製到UIView。這些圖像很大(有些是1024x768),我想確保我的繪圖代碼不是次優的。我在我的UIViewdrawRect方法是這樣的:這是將大圖像繪製到UIView的最佳方式嗎?

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(context, rect); 

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, 550); 
    CGContextConcatCTM(context, flipVertical); 
    CGImageRef shadowImage = [[UIImage imageNamed:@"someimage.png"] CGImage]; 
    CGContextDrawImage(context, CGRectMake(162, -174, 700, 700), shadowImage); 
    CGContextConcatCTM(context, flipVertical); 
} 

這裏有過做其它繪圖,但我只是想確保圖像繪製代碼不會導致問題,我沒有忽視了調用一個重要的方法或者錯誤地實現了這一點。

回答

3

我發現,這種方法是因爲使用的

[UIImage imageNamed:@"someimage.png"] 

這種方法將圖像加載到內存中,然後緩存供以後使用的次優的。由於我使用的圖像很大,並且其中有幾個,因此使用此方法會導致內存使用率過高。

我寫了一個short post更詳細。

+0

鏈接不起作用 – 2012-12-19 14:47:45

+0

@PooLaS固定 - 歡呼! – Codebeef 2012-12-20 11:55:12

+1

所以結論是使用'[UIImage imageWithContentsOfFile:路徑];'而不是'[UIImage imageNamed:@「someimage.png」];'line :) – 2012-12-20 12:02:52

相關問題