在你设置你的屏幕矩形,做一些像下面决定什么rect来绘制图像:
float hfactor = value.bounds.size.width / screenRect.size.width;
float vfactor = value.bounds.size.height / screenRect.size.height;
float factor = fmax(hfactor, vfactor);
// Divide the size by the greater of the vertical or horizontal shrinkage factor
float newWidth = value.bounds.size.width / factor;
float newHeight = value.bounds.size.height / factor;
// Then figure out if you need to offset it to center vertically or horizontally
float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;
CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
如果您不想放大比screenRect小的图像,请确保因子大于或等于1(例如factor = fmax(factor,1))。
要获得黑色背景,你可能只想将上下文颜色设置为黑色,并在绘制图像之前调用fillRect。