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;
}
Add this to method to UIView Category
Drop-in Swift 3.0 extension that supports the new iOS 10.0 API & the previous method.
Note:
!
which could cause a crash.Swift 3
The Swift 3 solution (based on Dima's answer) with UIView extension should be like this:
The current accepted answer is now out of date, at least if you are supporting iOS 7.
Here is what you should be using if you are only supporting iOS7+:
Swift 4:
As per this article, you can see that the new iOS7 method
drawViewHierarchyInRect:afterScreenUpdates:
is many times faster thanrenderInContext:
.Swift 3.0 implementation