UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; //截圖
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
在本代碼中,有兩個需要注意的有可能關系到內存的問題
- 內存問題,不會產生在
UIGraphicsGetImageFromCurrentImageContext()
這行代碼中
因為
UIGraphicsGetImageFromCurrentImageContext()
返回的是一個autorelease
的UIImage
對象
- 一定要對應
UIGraphicsBeginImageContextWithOptions()
和UIGraphicsEndImageContext ()
因為
UIGraphicsBeginImageContextWithOptions()
會在函數體內部通過
CGBitmapContetAlloc
內部函數分配內存空間,而UIGraphicsEndImageContext ()
的作用就是釋放該空間。