2012-08-08 13 views
20

Attualmente sto sviluppando un'applicazione semplice come Photoshop su iPhone. Quando voglio appiattire i miei livelli, le etichette sono nella posizione corretta ma con una dimensione del carattere errata. Ecco il mio codice per appiattire:Come creare un'immagine da UILabel?

UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument)); 

for (UILabel *label in arrayLabel) { 
    [label drawTextInRect:label.frame]; 
} 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Qualcuno mi può aiutare?

risposta

38

Da: pulling an UIImage from a UITextView or UILabel gives white image.

// iOS

- (UIImage *)grabImage { 
    // Create a "canvas" (image context) to draw in. 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0); // high res 
    // Make the CALayer to draw in our "canvas". 
    [[self layer] renderInContext: UIGraphicsGetCurrentContext()]; 

    // Fetch an UIImage of our "canvas". 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    // Stop the "canvas" from accepting any input. 
    UIGraphicsEndImageContext(); 

    // Return the image. 
    return image; 
} 

// Swift estensione w/utilizzo. credito @Heberti Almeida in seguito commenta

extension UIImage { 
    class func imageWithLabel(label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0) 
     label.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let img = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return img 
    } 
} 

L'utilizzo:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) 
label.text = bulletString 
let image = UIImage.imageWithLabel(label) 
+0

Sì, avevo visto questo post ma con questa soluzione non riesco ad ottenere un file ad alta definizione. Ma tu mi fai capire dov'è il mio problema! Grazie mille !! – Jonathan

+6

@DJPlayer Per ottenere un'immagine nitida per il display retina, dobbiamo sostituire la prima riga del metodo con questa riga: 'UIGraphicsBeginImageContextWithOptions (self.bounds.size, self.opaque, 0.0);' come visto in [questa domanda] (http://stackoverflow.com/q/4334233/2471006) e la sua risposta accettata. Forse potresti voler aggiornare la tua risposta, comprese quelle informazioni. – anneblue

+0

@anneblue grazie! – nemesis

5

è necessario per il rendering dell'etichetta in un contesto

sostituire

[label drawTextInRect:label.frame]; 

con

[label.layer renderInContext:UIGraphicsGetCurrentContext()]; 

e sarà necessario per creare immagini per etichetta, in modo da spostare il ciclo for per di fuori del contesto iniziare e terminare le chiamate

1

È possibile utilizzare renderInContext su qualsiasi CALayer. Disegna il livello della tua vista sul contesto. In modo che tu possa cambiarlo in un'immagine. Inoltre, se rendi renderInContext su una vista, tutte le sue sottoview verranno disegnate nel contesto. Quindi, invece di scorrere tutte le etichette, aggiungendo le etichette, è possibile aggiungerle a containerView. E solo bisogno di fare renderInContext su quel containerView.

12

ho creato un'estensione Swift per rendere il UILabel in un UIImage:

extension UIImage { 
    class func imageWithLabel(label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0) 
     label.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let img = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return img 
    } 
} 

EDIT: migliorata Swift 4 versione

extension UIImage { 
    class func imageWithLabel(_ label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0) 
     defer { UIGraphicsEndImageContext() } 
     label.layer.render(in: UIGraphicsGetCurrentContext()!) 
     return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage() 
    } 
} 

L'utilizzo è semplice:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) 
label.text = bulletString 
let image = UIImage.imageWithLabel(label) 
+1

Il mio buon signore è vero eroe! – rilar

+0

^^ Cosa ha detto ^^ – bkwebhero

1

per Swift , Io uso questa estensione UIView:

// Updated for Swift 3.0 
extension UIView { 
    var grabbedImage:UIImage? { 
     get { 
      UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) 
      layer.render(in: UIGraphicsGetCurrentContext()!) 
      let img = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      return img 
     } 
    } 
}