UIGraphicsBeginImageContext с параметрами
Я делаю снимок экрана в своем приложении. Я могу сделать снимок экрана.
теперь я хочу сделать снимок экрана, указав координаты x и y. Это возможно?
UIGraphicsBeginImageContext( self.view.bounds.size );
[self.view.layer renderInContext:UIGraphicsGetCurrentContext(  )];
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext(  );
            5 ответов
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Если вы используете новое устройство отображения retina, ваш код должен учитывать разрешение, используя UIGraphicsBeginImageContextWithOptions вместо UIGraphicsBeginImageContext:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
это отобразит контекст отображения изображения retina.
просто сделайте что-то вроде этого, намного проще, чем все эти сложные вычисления
+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}
вот Swift версия
//Capture Screen
func capture()->UIImage {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale)
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}
swift версия
    UIGraphicsBeginImageContext(self.view.bounds.size)
    let image: CGContextRef = UIGraphicsGetCurrentContext()!
    CGContextTranslateCTM(image, 0, -40)
    // <-- shift everything up by 40px when drawing.
    self.view.layer.renderInContext(image)
    let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)