2014-04-06 11 views
6

Desidero creare un nodo semplice con l'immagine profilo Facebook di un utente, in cui l'immagine ha angoli arrotondati (o un cerchio completo). Creo il nodo come segue:Come creare SKTexture con angoli arrotondati senza usare una maschera

SKNode *friend = [[SKNode alloc] init]; 

SKTexture *texture = [SKTexture textureWithImage:user[@"fbProfilePicture"]];     
SKSpriteNode *profilePic = [SKSpriteNode spriteNodeWithTexture:texture]; 

[friend addChild:profilePic]; 

non ho potuto trovare alcuna documentazione adeguata per creare l'immagine con gli angoli arrotondati, tranne usando SKCropNode (che sembra essere una cattiva soluzione)

+0

Perché non creare un nodo a due livelli: avatar utente + immagine png? – AndrewShmig

+0

Da quando prendo l'immagine da Facebook, devo ritagliarla quando la ottengo. Hai un suggerimento su come farlo accadere? – Yuvals

risposta

5

Prova questo:

// your profile picture 
UIImage *fbProfilePicture = [UIImage imageNamed:@"fbProfilePicture"]; 

// create the image with rounded corners 
UIGraphicsBeginImageContextWithOptions(fbProfilePicture.size, NO, 0); 
CGRect rect = CGRectMake(0, 0, fbProfilePicture.size.width, fbProfilePicture.size.height); 
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:20.0] addClip]; 
[fbProfilePicture drawInRect:rect]; 
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// use the roundedImage as texture for your sprite  
SKTexture *texture = [SKTexture textureWithImage:roundedImage]; 
SKSpriteNode *profilePic = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(fbProfilePicture.size.width, fbProfilePicture.size.height)]; 

[self addChild:profilePic]; 

L'angolo arrotondato parte da this answer.

+0

Molto bene, questo fa esattamente quello di cui avevo bisogno! – Yuvals

6

Ecco come appare in Swift traducendo la risposta sopra, la risposta di Sebastian. Il metodo riceve il nome dell'immagine e restituisce un nodo con gli angoli arrotondati.

class func roundSquareImage(imageName: String) -> SKSpriteNode { 
      let originalPicture = UIImage(named: imageName) 
      // create the image with rounded corners 
      UIGraphicsBeginImageContextWithOptions(originalPicture!.size, false, 0) 
      let rect = CGRectMake(0, 0, originalPicture!.size.width, originalPicture!.size.height) 
      let rectPath : UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 30.0) 
      rectPath.addClip() 
      originalPicture!.drawInRect(rect) 
      let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext(); 

      let texture = SKTexture(image: scaledImage) 
      let roundedImage = SKSpriteNode(texture: texture, size: CGSizeMake(originalPicture!.size.width, originalPicture!.size.height)) 
      roundedImage.name = imageName 
      return roundedImage 
     } 
+0

Qualche prestazione riguarda questo approccio? Grazie per la traduzione! – Crashalot

1

per il 2017 ...

class YourSprite: SKSpriteNode { 

    func yourSetupFunction() { 

      texture = SKTexture(image: UIImage(named: "cat")!.circleMasked!) 

E 'così facile ...

enter image description here

Codice per circleMasked:

(Tutti i progetti che si occupano di immagini sarà bisogno comunque di questo.)

extension UIImage { 

    var isPortrait: Bool { return size.height > size.width } 
    var isLandscape: Bool { return size.width > size.height } 
    var breadth:  CGFloat { return min(size.width, size.height) } 
    var breadthSize: CGSize { return CGSize(width: breadth, height: breadth) } 
    var breadthRect: CGRect { return CGRect(origin: .zero, size: breadthSize) } 

    var circleMasked: UIImage? { 

     UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale) 
     defer { UIGraphicsEndImageContext() } 

     guard let cgImage = cgImage?.cropping(to: CGRect(origin: 
      CGPoint(
       x: isLandscape ? floor((size.width - size.height)/2) : 0, 
       y: isPortrait ? floor((size.height - size.width)/2) : 0), 
      size: breadthSize)) 
     else { return nil } 

     UIBezierPath(ovalIn: breadthRect).addClip() 
     UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation) 
      .draw(in: breadthRect) 
     return UIGraphicsGetImageFromCurrentImageContext() 
    } 

// classic 'circleMasked' stackoverflow fragment 
// courtesy Leo Dabius /a/29047372/294884 
}