我想通過使用CGImage
的瓷磚來繪製CGContext
,我想以指定的順序繪製。從CGImage在CGContext上繪圖
根據文檔here似乎唯一方法在圖形上下文繪製圖像是
void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);
但是,這並不需要在賬戶中,我只想畫一個CGRect
的情況下,源圖像。我需要做一些技巧才能做到這一點嗎?
我想通過使用CGImage
的瓷磚來繪製CGContext
,我想以指定的順序繪製。從CGImage在CGContext上繪圖
根據文檔here似乎唯一方法在圖形上下文繪製圖像是
void CGContextDrawImage (
CGContextRef c,
CGRect rect,
CGImageRef image
);
但是,這並不需要在賬戶中,我只想畫一個CGRect
的情況下,源圖像。我需要做一些技巧才能做到這一點嗎?
如果我正確理解你的問題,那麼你需要在特定的CGRect
上裁剪圖像,如果是的話,那麼你需要看看這種方法。
CGImageCreateWithImageInRect(CGImage image, CGRect rect)
這將返回一個CGImageRef
並根據您的需要,我認爲這是你在找什麼。
這是從蘋果的Quartz 2D Programming Guide上市11-1:
myImageArea = CGRectMake (rooster_head_x_origin, rooster_head_y_origin,
myWidth, myHeight);
mySubimage = CGImageCreateWithImageInRect (myRoosterImage, myImageArea);
myRect = CGRectMake(0, 0, myWidth*2, myHeight*2);
CGContextDrawImage(context, myRect, mySubimage);
注意CGImageCreateWithImageInRect
回報,你是負責釋放的引用,所以你必須要做到這一點,當你與完成mySubimage
:
CGImageRelease(mySubimage);
注意,結果保留原始圖像,雖然它比圖像繪製到上下文更快,它可能會佔用更多的內存。 –