不用 2d截圖 直接截圖
-(UIImage *)screenShots
{
//擷取整個backview
UIGraphicsBeginImageContext(self.backgroundView.bounds.size);
[self.backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//在截圖上畫下自己需要的位置及大小
UIGraphicsBeginImageContext(self.mainContentView.frame.size);
[sourceImage drawAtPoint:CGPointMake(0, self.mainContentView.bounds.size.height - self.view.bounds.size.height)];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
return image;
}
複製程式碼
上面的方法會壓縮圖片,用下面的方法可以設定是否縮放圖片
UIGraphicsBeginImageContextWithOptions( self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
複製程式碼