objective iphone cocoa-touch uiimageview uiimage antialiasing

iphone - objective - uiimageview set image swift



¿Alguna técnica de suavizado rápido y sucio para UIImageView girado? (8)

Encontré esta solución desde here , y es perfecta:

+ (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; }

uso:

UIImage *image = [UIImage renderImageFromView:view withRect:view.bounds transparentInsets:UIEdgeInsetsZero];

Tengo un UIImageView (fotograma completo y rectangular) que estoy rotando con CGAffineTransform. El UIImage del UIImageView llena el cuadro completo. Cuando la imagen se gira y dibuja, los bordes aparecen marcadamente irregulares. ¿Hay algo que pueda hacer para que se vea mejor? Claramente, no está siendo antialiasado con el fondo.


La mejor manera que he encontrado para tener bordes suaves y una imagen nítida es hacer esto:

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();

Agregar la clave Info.plist como algunas personas describen tiene un gran impacto en el rendimiento y, si la usas, básicamente la aplicas a todo en lugar de a un solo lugar donde la necesites.

Además, no use UIGraphicsBeginImageContext(imageRect.size); de lo contrario, la capa estará borrosa. Tienes que usar UIGraphicsBeginImageContextWithOptions como he mostrado.


Los bordes de las capas de CoreAnimation no tienen antialias por defecto en iOS. Sin embargo, hay una clave que puede establecer en Info.plist que habilita el antialiasing de los bordes: UIViewEdgeAntialiasing.

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

Si no desea que la sobrecarga de rendimiento habilite esta opción, una solución alternativa es agregar un borde transparente de 1 píxel alrededor del borde de la imagen . Esto significa que los "bordes" de la imagen ya no están en el borde, por lo que no es necesario un tratamiento especial.



Recuerde establecer las opciones de anti-alias apropiadas:

CGContextSetAllowsAntialiasing(theContext, true); CGContextSetShouldAntialias(theContext, true);


simplemente agregue "Renders with edge antialiasing" con YES in plist y funcionará.


solo agrega un borde transparente de 1 píxel a tu imagen

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();


Nueva API - iOS 6/7

También funciona para iOS 6, como señaló @Chris, pero no se hizo público hasta iOS 7.

Desde iOS 7, CALayer tiene una nueva propiedad allowsEdgeAntialiasing que hace exactamente lo que usted desea en este caso, sin incurrir en la sobrecarga de habilitarlo para todas las vistas en su aplicación. Esta es una propiedad de CALayer, por lo que para habilitar esto para una UIView usted usa myView.layer.allowsEdgeAntialiasing = YES .