My code works fine for normal devices but creates blurry images on retina devices.
Does anybody know a solution for my issue?
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Here's a Swift 4 UIView extension based on the answer from @Dima.
To improve answers by @Tommy and @Dima, use the following category to render UIView into UIImage with transparent background and without loss of quality. Working on iOS7. (Or just reuse that method in implementation, replacing
self
reference with your image)UIView+RenderViewToImage.h
UIView+RenderViewToImage.m
I have created a Swift extension based on @Dima solution:
EDIT: Swift 4 improved version
Usage: