Изображение с UIView

Есть ли возможность создать изображение из UIView?

Спасибо, Andreas

Ответы

Ответ 1

#import "QuartzCore/QuartzCore.h" после добавления рамки к вашему проекту. Затем выполните:

UIGraphicsBeginImageContext(yourView.frame.size);
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// The result is *screenshot

Ответ 2

Я использовал ответ, чтобы улучшить его еще больше. К сожалению, исходный код создавал только зеркальное изображение.

Итак, здесь рабочий код:

- (UIImage *) imageFromView:(UIView *)view {

    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(currentContext, 0, view.size.height);
    // passing negative values to flip the image
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    [[appDelegate.scatterPlotView layer] renderInContext:currentContext];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshot;
}