Io uso una vista dell'immagine:L'immagine ruotata viene distorta e sfocata?
@IBOutlet weak var imageView: UIImageView!
a dipingere un'immagine e anche un'altra immagine che è stata ruotata. Si scopre che l'immagine ruotata ha una pessima qualità. Nell'immagine seguente gli occhiali nella scatola gialla non sono ruotati. Gli occhiali nella scatola rossa sono ruotati di 4,39 gradi.
Ecco il codice che uso per disegnare gli occhiali:
UIGraphicsBeginImageContext(imageView.image!.size)
imageView.image!.drawInRect(CGRectMake(0, 0, imageView.image!.size.width, imageView.image!.size.height))
var drawCtxt = UIGraphicsGetCurrentContext()
var glassImage = UIImage(named: "glasses.png")
let yellowRect = CGRect(...)
CGContextSetStrokeColorWithColor(drawCtxt, UIColor.yellowColor().CGColor)
CGContextStrokeRect(drawCtxt, yellowRect)
CGContextDrawImage(drawCtxt, yellowRect, glassImage!.CGImage)
// paint the rotated glasses in the red square
CGContextSaveGState(drawCtxt)
CGContextTranslateCTM(drawCtxt, centerX, centerY)
CGContextRotateCTM(drawCtxt, 4.398 * CGFloat(M_PI)/180)
var newRect = yellowRect
newRect.origin.x = -newRect.size.width/2
newRect.origin.y = -newRect.size.height/2
CGContextAddRect(drawCtxt, newRect)
CGContextSetStrokeColorWithColor(drawCtxt, UIColor.redColor().CGColor)
CGContextSetLineWidth(drawCtxt, 1)
// draw the red rect
CGContextStrokeRect(drawCtxt, newRect)
// draw the image
CGContextDrawImage(drawCtxt, newRect, glassImage!.CGImage)
CGContextRestoreGState(drawCtxt)
Come posso ruotare e dipingere i vetri senza perdita di qualità o di ottenere un'immagine distorta?
Aiuta a creare il contesto con 'UIGraphicsBeginImageContextWithOptions (..., 0.0)' per ottenere un contesto di disegno che utilizza lo stesso fattore di scala del dispositivo? –
@MartinR quale taglia dovrei usare per generare il contesto? –