Domanda

Ho un UIImageView (full frame e rettangolare) che sto rotante con CGAffineTransform. L'UIImage del UIImageView riempie l'intero fotogramma. Quando l'immagine viene ruotata e disegnato i bordi appaiono visibilmente frastagliati. C'è qualcosa che posso fare per renderla migliore? Non è chiaramente essere anti-alias con lo sfondo.

È stato utile?

Soluzione

Ricordate di impostare le opzioni anti-alias appropriate:

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

Altri suggerimenti

I bordi degli strati Core Animation non sono antialias per impostazione predefinita in iOS. Tuttavia, v'è una chiave che è possibile impostare in Info.plist che abilita l'antialias dei bordi:. UIViewEdgeAntialiasing

https://developer.apple .com / library / content / documentazione / General / Reference / InfoPlistKeyReference / articoli / iPhoneOSKeys.html

Se non si desidera che il sovraccarico delle prestazioni di attivazione di questa opzione, un work-around è quello di aggiungere un bordo 1px trasparente attorno al bordo dell'immagine . Ciò significa che i 'bordi' dell'immagine non sono più ai margini, quindi non hanno bisogno di un trattamento speciale!

Nuova API - iOS 6/7

funziona anche per iOS 6, come notato da @ Chris, ma non è stato reso pubblico fino a iOS 7.

Dato che iOS 7, CALayer ha un nuovo allowsEdgeAntialiasing proprietà che fa esattamente ciò che si desidera in questo caso, senza incorrere l'overhead di consentire per tutte le viste nella vostra applicazione! Questa è una proprietà di CALayer, in modo da consentire questo per un UIView si utilizza myView.layer.allowsEdgeAntialiasing = YES.

basta aggiungere confine 1px trasparente per l'immagine

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

basta aggiungere "Rende con antialiasing bordo" con YES in plist e funzionerà.

Consiglio caldamente la seguente libreria.

http: // vocaro .com / Trevor / blog / 2009/10/12 / ridimensionare-a-UIImage-la-destra-way /

Esso contiene un sacco di utili estensioni per UIImage che risolvono questo problema e anche contenenti codice per la generazione di miniature, ecc.

Enjoy!

Il modo migliore che ho trovato per avere bordi lisci e un'immagine nitida è per fare questo:

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

L'aggiunta della chiave Info.plist come alcune persone descrivere ha un grande successo sulle prestazioni e se si utilizza che allora si sta fondamentalmente applicarlo a tutto ciò invece di l'unico posto che si ha bisogno.

Inoltre, non basta usare UIGraphicsBeginImageContext(imageRect.size); altrimenti lo strato sarà sfocata. Devi usare UIGraphicsBeginImageContextWithOptions come ho mostrato.

Ho trovato questa soluzione da qui , ed è perfetto:

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

utilizzo:

UIImage *image = [UIImage renderImageFromView:view withRect:view.bounds transparentInsets:UIEdgeInsetsZero];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top