用UIGraphics進行2D圖像渲染 不要用UIGraphicsBeginImageContext(size); 不然圖片會模糊
Core Graphics API所有的操作都在一個上下文中進行。所以在繪圖之前需要獲取該上下文并傳入執(zhí)行渲染的函數(shù)中。如果你正在渲染一副在內(nèi)存中的圖片,此時就需要傳入圖片所屬的上下文。獲得一個圖形上下文是我們完成繪圖任務(wù)的第一步,你可以將圖形上下文理解為一塊畫布。如果你沒有得到這塊畫布,那么你就無法完成任何繪圖操作。當(dāng)然,有許多方式獲得一個圖形上下文,這里我介紹兩種最為常用的獲取方法。
調(diào)用UIGraphicsBeginImageContextWithOptions函數(shù)就可獲得用來處理圖片的圖形上下文。利用該上下文,你就可以在其上進行繪圖,并生成圖片。調(diào)用UIGraphicsGetImageFromCurrentImageContext函數(shù)可從當(dāng)前上下文中獲取一個UIImage對象。記住在你所有的繪圖操作后別忘了調(diào)用UIGraphicsEndImageContext函數(shù)關(guān)閉圖形上下文。
上代碼
- ( UIImage *)createShareImage:( NSString *)str of:(UIImage *)isImage
{
UIImage *image = isImage;
CGSize size= CGSizeMake (image. size . width , image. size . height ); // 畫布大小
UIGraphicsBeginImageContextWithOptions (size, NO , 0.0 );
[image drawAtPoint : CGPointMake ( 0 , 0 )];
// 獲得一個位圖圖形上下文
CGContextRef context= UIGraphicsGetCurrentContext ();
CGContextDrawPath (context, kCGPathStroke );
// 畫 打敗了多少用戶
[str drawAtPoint : CGPointMake ( 50 , image.size.height - 100 ) withAttributes : @{ NSFontAttributeName :[ UIFont fontWithName : @"Arial-BoldMT" size : 70 ], NSForegroundColorAttributeName :[ UIColor whiteColor ] } ];
//畫自己想畫的內(nèi)容。。。。。
// 返回繪制的新圖形
UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
return newImage;
}