我已经得到了我和一个CGAffineTransform旋转一个UIImageView(全帧和矩形)。 所述的UIImageView的UIImage的填充整个帧。 当图像被旋转和绘制的边缘出现明显锯齿状。 有什么我可以做,使之更好看? 这显然不是抗锯齿与背景。
Answer 1:
请记住,设置适当的抗混叠选项:
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
Answer 2:
CoreAnimation层的边缘不是默认iOS上的抗锯齿。 但是,你可以在Info.plist中设置一个键,使边缘抗锯齿:UIViewEdgeAntialiasing。
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
如果你不希望启用该选项的性能开销,一个解决办法是到周围添加图像边缘的1px的透明边框 。 这意味着图像的“边缘”都在边缘不再,所以不需要特殊处理!
Answer 3:
新的API - iOS的6/7
也适用于iOS 6,如@克里斯指出,但是并没有公开,直到iOS的7。
由于iOS的7,CALayer的有一个新的属性allowsEdgeAntialiasing
这不正是你在这种情况下想要的东西,而不会产生使之在你的应用程序的所有意见的开销! 这是的CALayer的属性,所以启用此为你使用一个UIView myView.layer.allowsEdgeAntialiasing = YES
。
Answer 4:
只需添加1px的透明边框图像
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Answer 5:
只需添加“与边缘抗锯齿渲染”在plist中YES,它会工作。
Answer 6:
我会完全推荐以下库。
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
它包含了许多有用的扩展到UIImage的是解决这个问题,也包括代码生成缩略图等。
请享用!
Answer 7:
我发现有平滑的边缘和锐利的图像,最好的办法是做这样的:
CGRect imageRect = CGRectMake(0, 0, self.photo.image.size.width, self.photo.image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[self.photo.image drawInRect:CGRectMake(1, 1, self.photo.image.size.width - 2, self.photo.image.size.height - 2)];
self.photo.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
添加像一些人的Info.plist键描述对性能有很大的打击,如果你使用那么你基本上把它应用到一切,而不只是一个地方,你需要它。
另外,不只是用UIGraphicsBeginImageContext(imageRect.size);
否则层将变得模糊。 你必须使用UIGraphicsBeginImageContextWithOptions
像我展示。
Answer 8:
我发现从该解决方案在这里 ,它是完美的:
+ (UIImage *)renderImageFromView:(UIView *)view withRect:(CGRect)frame transparentInsets:(UIEdgeInsets)insets {
CGSize imageSizeWithBorder = CGSizeMake(frame.size.width + insets.left + insets.right, frame.size.height + insets.top + insets.bottom);
// Create a new context of the desired size to render the image
UIGraphicsBeginImageContextWithOptions(imageSizeWithBorder, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Clip the context to the portion of the view we will draw
CGContextClipToRect(context, (CGRect){{insets.left, insets.top}, frame.size});
// Translate it, to the desired position
CGContextTranslateCTM(context, -frame.origin.x + insets.left, -frame.origin.y + insets.top);
// Render the view as image
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Fetch the image
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
// Cleanup
UIGraphicsEndImageContext();
return renderedImage;
}
用法:
UIImage *image = [UIImage renderImageFromView:view withRect:view.bounds transparentInsets:UIEdgeInsetsZero];